diff --git a/powerbell/build.properties b/powerbell/build.properties index be4a8199..cb8978e7 100644 --- a/powerbell/build.properties +++ b/powerbell/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Fri Nov 28 18:45:48 GMT 2025 +#Sat Nov 29 02:59:10 GMT 2025 stageCount=11 libraryProject= baseVersion=15.11 publishVersion=15.11.10 -buildCount=29 +buildCount=32 baseBetaVersion=15.11.11 diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java index fd0b80de..32cf4346 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java @@ -25,7 +25,7 @@ import cc.winboll.studio.powerbell.activities.WinBoLLActivity; import cc.winboll.studio.powerbell.beans.BackgroundPictureBean; import cc.winboll.studio.powerbell.fragments.MainViewFragment; import cc.winboll.studio.powerbell.unittest.MainUnitTestActivity; -import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils; +import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils; /** * 主活动类(修复小米广告SDK空Context崩溃问题) @@ -281,7 +281,7 @@ public class MainActivity extends WinBoLLActivity { if (isFinishing() || isDestroyed()) { return; } - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); int nPixelColor = bean.getPixelColor(); RelativeLayout mainLayout = findViewById(R.id.activitymainRelativeLayout1); diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundPictureActivity.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundPictureActivity.java index c26381cb..37b8e63e 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundPictureActivity.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundPictureActivity.java @@ -25,7 +25,7 @@ import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.beans.BackgroundPictureBean; import cc.winboll.studio.powerbell.dialogs.BackgroundPicturePreviewDialog; import cc.winboll.studio.powerbell.dialogs.NetworkBackgroundDialog; -import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils; +import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils; import cc.winboll.studio.powerbell.utils.FileUtils; import cc.winboll.studio.powerbell.utils.UriUtil; import cc.winboll.studio.powerbell.views.BackgroundView; @@ -40,7 +40,7 @@ import androidx.core.util.Preconditions; public class BackgroundPictureActivity extends WinBoLLActivity implements BackgroundPicturePreviewDialog.IOnRecivedPictureListener { public static final String TAG = "BackgroundPictureActivity"; - public BackgroundPictureUtils mBackgroundPictureUtils; + public BackgroundSourceUtils mBackgroundPictureUtils; // 图片选择请求码 public static final int REQUEST_SELECT_PICTURE = 0; @@ -55,7 +55,8 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr private File mfRecivedPicture; // 接收的图片文件 // 背景视图预览图片的文件名 - private String preViewFilePathBackgroundView = ""; + private String preViewFilePath = ""; + private String preViewFileUrl = ""; BackgroundView bvPreviewBackground; boolean isCommitSettings = false; @@ -72,7 +73,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr private static String _mSourceCroppedFilePath; private static String _mszCommonFileType = "jpeg"; private int mnPictureCompress = 100; - private static String _RecivedPictureFileName; + private static String _RecivedBackgroundFileName; @Override public Activity getActivity() { @@ -91,7 +92,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr initEnv(); // 初始化工具类和文件夹 - mBackgroundPictureUtils = BackgroundPictureUtils.getInstance(this); + mBackgroundPictureUtils = BackgroundSourceUtils.getInstance(this); mfBackgroundDir = new File(mBackgroundPictureUtils.getBackgroundDir()); if (!mfBackgroundDir.exists()) { mfBackgroundDir.mkdirs(); @@ -149,7 +150,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr private void initEnv() { LogUtils.d(TAG, "initEnv()"); - _RecivedPictureFileName = "SourcePicture.data"; + _RecivedBackgroundFileName = "SourcePicture.data"; } public static String getBackgroundFileName() { @@ -158,7 +159,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr @Override public void onAcceptRecivedPicture(String szPreRecivedPictureName) { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this); utils.getBackgroundPictureBean().setIsUseBackgroundFile(true); utils.saveData(); @@ -178,7 +179,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr LogUtils.d(TAG, String.format("_mSourceCroppedFilePath : %s", _mSourceCroppedFilePath)); //ImageView ivPreviewBackground = (ImageView) findViewById(R.id.activitybackgroundpictureImageView1); bvPreviewBackground = (BackgroundView) findViewById(R.id.activitybackgroundpictureBackgroundView1); - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this); utils.loadBackgroundPictureBean(); boolean isUseBackgroundFile = utils.getBackgroundPictureBean().isUseBackgroundFile(); @@ -188,9 +189,9 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr if (_mSourceCroppedFile.exists()) { //try { //String filePath = utils.getBackgroundDir() + getBackgroundFileName(); - preViewFilePathBackgroundView = _mSourceCroppedFilePath; - LogUtils.d(TAG, String.format("preViewFilePathBackgroundView : %s", preViewFilePathBackgroundView)); - bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView); + preViewFilePath = _mSourceCroppedFilePath; + LogUtils.d(TAG, String.format("preViewFilePathBackgroundView : %s", preViewFilePath)); + bvPreviewBackground.previewBackgroundImage(preViewFilePath); /*Drawable drawable = FileUtils.getImageDrawable(filePath); if (drawable != null) { //drawable.setAlpha(120); @@ -203,8 +204,8 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr // } } else { ToastUtils.show("未使用背景图片"); - preViewFilePathBackgroundView = ""; - bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView); + preViewFilePath = ""; + bvPreviewBackground.previewBackgroundImage(preViewFilePath); // Drawable drawable = getResources().getDrawable(R.drawable.blank10x10); // if (drawable != null) { // drawable.setAlpha(120); @@ -217,13 +218,13 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() { @Override public void onClick(View v) { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); bean.setIsUseBackgroundFile(false); utils.saveData(); - preViewFilePathBackgroundView = ""; - bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView); + preViewFilePath = ""; + bvPreviewBackground.previewBackgroundImage(preViewFilePath); } }; @@ -288,7 +289,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() { @Override public void onClick(View v) { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this); utils.getBackgroundPictureBean().setIsUseBackgroundFile(true); utils.saveData(); updatePreviewBackground(); @@ -311,7 +312,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() { @Override public void onClick(View v) { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); bean.setPixelColor(0); utils.saveData(); @@ -446,27 +447,24 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr fos.flush(); if (success) { //ToastUtils.show("保存成功"); - // 更新数据 -// BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this); -// BackgroundPictureBean bean = utils.getBackgroundPictureBean(); -// bean.setIsUseBackgroundFile(true); -// utils.saveData(); updatePreviewBackground(); } else { + LogUtils.e(TAG, "图片压缩保存失败"); ToastUtils.show("图片压缩保存失败"); } } catch (FileNotFoundException e) { LogUtils.e(TAG, "文件未找到" + e); - ToastUtils.show("保存失败:文件路径错误"); + ToastUtils.show("文件未找到" + e); } catch (IOException e) { LogUtils.e(TAG, "写入异常" + e); - ToastUtils.show("保存失败:磁盘可能已满或路径错误"); + ToastUtils.show("写入异常" + e); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { LogUtils.e(TAG, "流关闭异常" + e); + ToastUtils.show("流关闭异常" + e); } } if (scaledBitmap != null && !scaledBitmap.isRecycled()) { @@ -500,15 +498,16 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr } public File getRecivedPictureFile() { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this); utils.loadBackgroundPictureBean(); - return new File(utils.getBackgroundDir(), _RecivedPictureFileName); + return new File(utils.getBackgroundDir(), _RecivedBackgroundFileName); } - public void saveToRecivedPictureFile(String srcFilePath) { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this); + public void saveToRecivedBackground(String srcFilePath, String srcFillSourcePath) { + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this); utils.loadBackgroundPictureBean(); - File dstFile = new File(utils.getBackgroundDir(), _RecivedPictureFileName); + File dstFile = new File(utils.getBackgroundDir(), _RecivedBackgroundFileName); + compressQualityToRecivedPicture(srcFilePath); FileUtils.copyFile(new File(srcFilePath), dstFile); } @@ -619,7 +618,7 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr } void setBackgroundColor() { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); int nPixelColor = bean.getPixelColor(); RelativeLayout mainLayout = findViewById(R.id.activitybackgroundpictureRelativeLayout1); @@ -636,27 +635,17 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr // 在需要显示对话框的地方(如网络状态监听回调中) NetworkBackgroundDialog dialog = new NetworkBackgroundDialog(this, new NetworkBackgroundDialog.OnDialogClickListener() { @Override - public void onConfirm(String szConfirmFilePath) { + public void onConfirm(String szConfirmFilePath, String szConfirmFileUrl) { //ToastUtils.show("onConfirm"); - onRecivedPictureListener.onRecivedPicture(szConfirmFilePath); - - //((BackgroundPicturePreviewDialog.IOnRecivedPictureListener)this).onAcceptRecivedPicture(szConfirmFilePath); - - // 设置预览图 - preViewFilePathBackgroundView = szConfirmFilePath; - bvPreviewBackground.previewBackgroundImage(preViewFilePathBackgroundView); - - // 处理确认逻辑(如允许后台网络使用) - LogUtils.d("MainActivity", "用户允许后台网络使用"); - // 执行具体业务:如开启后台网络请求服务 + // 保存预览资源信息 + preViewFilePath = szConfirmFilePath; + preViewFileUrl = szConfirmFileUrl; + onRecivedPictureListener.onRecivedPicture(preViewFilePath, preViewFileUrl); } @Override public void onCancel() { //ToastUtils.show("onCancel"); - // 处理取消逻辑(如禁止后台网络使用) - LogUtils.d("MainActivity", "用户禁止后台网络使用"); - // 执行具体业务:如关闭后台网络请求 } }); @@ -670,13 +659,13 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr } interface OnRecivedPictureListener { - void onRecivedPicture(String srcFilePath); + void onRecivedPicture(String srcFilePath, String srcFileUrl); } OnRecivedPictureListener onRecivedPictureListener = new OnRecivedPictureListener(){ @Override - public void onRecivedPicture(String srcFilePath) { - saveToRecivedPictureFile(srcFilePath); + public void onRecivedPicture(String srcFilePath, String srcFileUrl) { + saveToRecivedBackground(srcFilePath, srcFileUrl); startCropImageActivity(true); } }; @@ -692,16 +681,16 @@ public class BackgroundPictureActivity extends WinBoLLActivity implements Backgr @Override public void onNo() { isCommitSettings = true; - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(BackgroundPictureActivity.this); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); - bean.setIsUseBackgroundFile(!preViewFilePathBackgroundView.equals("")); + bean.setIsUseBackgroundFile(!preViewFilePath.equals("")); utils.saveData(); finish(); } @Override public void onYes() { - bvPreviewBackground.setImageViewSource(preViewFilePathBackgroundView); + bvPreviewBackground.setImageViewSource(preViewFilePath); isCommitSettings = true; finish(); } diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/PixelPickerActivity.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/PixelPickerActivity.java index 85fd970c..c73b46a7 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/PixelPickerActivity.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/PixelPickerActivity.java @@ -27,7 +27,7 @@ import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity; import cc.winboll.studio.powerbell.activities.PixelPickerActivity; import cc.winboll.studio.powerbell.beans.BackgroundPictureBean; -import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils; +import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -193,7 +193,7 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi public void onClick(View v) { dialog.dismiss(); // 可以在这里添加确定后的回调逻辑 - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(PixelPickerActivity.this); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); bean.setPixelColor(pixelColor); utils.saveData(); @@ -217,7 +217,7 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi void setBackgroundColor() { - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(PixelPickerActivity.this); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); int nPixelColor = bean.getPixelColor(); RelativeLayout mainLayout = findViewById(R.id.activitypixelpickerRelativeLayout1); diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/BackgroundPicturePreviewDialog.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/BackgroundPicturePreviewDialog.java index ef7a4449..30be0778 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/BackgroundPicturePreviewDialog.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/BackgroundPicturePreviewDialog.java @@ -13,7 +13,7 @@ import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.powerbell.MainActivity; import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity; -import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils; +import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils; import cc.winboll.studio.powerbell.utils.FileUtils; import cc.winboll.studio.powerbell.utils.UriUtil; import java.io.File; @@ -29,7 +29,7 @@ public class BackgroundPicturePreviewDialog extends Dialog { public static final String TAG = "BackgroundPicturePreviewDialog"; Context mContext; - BackgroundPictureUtils mBackgroundPictureUtils; + BackgroundSourceUtils mBackgroundPictureUtils; Button dialogbackgroundpicturepreviewButton1; Button dialogbackgroundpicturepreviewButton2; String mszPreReceivedFileName; diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java index e8416dc3..0be5eb2a 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java @@ -15,11 +15,11 @@ import androidx.appcompat.app.AlertDialog; import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.libappbase.ToastUtils; import cc.winboll.studio.powerbell.R; -import cc.winboll.studio.powerbell.utils.PictureUtils; import cc.winboll.studio.powerbell.views.BackgroundView; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import cc.winboll.studio.powerbell.utils.ImageDownloader; /** * @Author ZhanGSKen&豆包大模型 @@ -46,10 +46,11 @@ public class NetworkBackgroundDialog extends AlertDialog { // 主线程 Handler,用于接收子线程消息并更新 UI private Handler mUiHandler; String previewFilePath; + String previewFileUrl; // 按钮点击回调接口(Java7 接口实现) public interface OnDialogClickListener { - void onConfirm(String szConfirmFilePath); // 确认按钮点击 + void onConfirm(String szConfirmFilePath, String previewFileUrl); // 确认按钮点击 void onCancel(); // 取消按钮点击 } @@ -87,8 +88,8 @@ public class NetworkBackgroundDialog extends AlertDialog { switch (msg.what) { case MSG_IMAGE_LOAD_SUCCESS: // 图片加载成功,获取文件路径并设置背景 - String filePath = (String) msg.obj; - setBackgroundFromPath(filePath); + previewFilePath = (String) msg.obj; + setBackgroundFromPath(previewFilePath); break; case MSG_IMAGE_LOAD_FAILED: // 图片加载失败,设置默认背景 @@ -135,7 +136,8 @@ public class NetworkBackgroundDialog extends AlertDialog { btnPreview = (Button) dialogView.findViewById(R.id.btn_preview); etURL = (EditText) dialogView.findViewById(R.id.et_url); bvBackgroundPreview = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview); - + // 加载初始图片 + bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher); // 设置按钮点击事件 setButtonClickListeners(); } @@ -166,7 +168,7 @@ public class NetworkBackgroundDialog extends AlertDialog { dismiss(); // 关闭对话框 if (listener != null) { - listener.onConfirm(previewFilePath); + listener.onConfirm(previewFilePath, previewFileUrl); } } }); @@ -175,14 +177,7 @@ public class NetworkBackgroundDialog extends AlertDialog { btnPreview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - LogUtils.d("NetworkBackgroundDialog", "确认预览点击"); downloadImageToAlbumAndPreview(); - /*String url = etURL.getText().toString().trim(); - if (url.isEmpty()) { - ToastUtils.show("请输入图片链接"); - return; - } - ImageDownloader.getInstance(mContext).downloadImage(url, mDownloadCallback);*/ } }); } @@ -197,8 +192,7 @@ public class NetworkBackgroundDialog extends AlertDialog { File imageFile = new File(filePath); if (!imageFile.exists()) { LogUtils.e(TAG, "图片文件不存在:" + filePath); - ToastUtils.show("Test"); - //bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher); + bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher); return; } @@ -249,29 +243,10 @@ public class NetworkBackgroundDialog extends AlertDialog { this.listener = listener; } - /*ImageDownloader.DownloadCallback mDownloadCallback = new ImageDownloader.DownloadCallback() { - @Override - public void onSuccess(String filePath) { - ToastUtils.show("图片下载成功:" + filePath); - LogUtils.d(TAG, filePath); - // 发送消息到主线程,携带图片路径 - Message successMsg = mUiHandler.obtainMessage(MSG_IMAGE_LOAD_SUCCESS, filePath); - mUiHandler.sendMessage(successMsg); - } - - @Override - public void onFailure(String errorMsg) { - ToastUtils.show("下载失败:" + errorMsg); - LogUtils.e(TAG, errorMsg); - // 发送图片加载失败消息 - mUiHandler.sendEmptyMessage(MSG_IMAGE_LOAD_FAILED); - } - };*/ - void downloadImageToAlbumAndPreview() { - //String imgUrl = "https://example.com/test.jpg"; - String imgUrl = etURL.getText().toString(); - PictureUtils.downloadImageToAlbum(mContext, imgUrl, new PictureUtils.DownloadCallback(){ + //String previewFileUrl = "https://example.com/test.jpg"; + previewFileUrl = etURL.getText().toString(); + ImageDownloader.getInstance(mContext).downloadImage(previewFileUrl, new ImageDownloader.DownloadCallback(){ @Override public void onSuccess(String savePath) { ToastUtils.show("下载成功:" + savePath); @@ -281,8 +256,8 @@ public class NetworkBackgroundDialog extends AlertDialog { } @Override - public void onFailure(Exception e) { - ToastUtils.show("下载失败:" + e.getMessage()); + public void onFailure(String errorMsg) { + ToastUtils.show("下载失败:" + errorMsg); } }); diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java index bc44ca2a..313f5a8b 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java @@ -23,7 +23,7 @@ import cc.winboll.studio.powerbell.activities.PixelPickerActivity; import cc.winboll.studio.powerbell.beans.BackgroundPictureBean; import cc.winboll.studio.powerbell.services.ControlCenterService; import cc.winboll.studio.powerbell.utils.AppConfigUtils; -import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils; +import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils; import cc.winboll.studio.powerbell.utils.ServiceUtils; import cc.winboll.studio.powerbell.views.BackgroundView; import cc.winboll.studio.powerbell.views.BatteryDrawable; @@ -83,7 +83,7 @@ public class MainViewFragment extends Fragment { // 获取指定ID的View实例 bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1); - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity()); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(getActivity()); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); int nPixelColor = bean.getPixelColor(); bvPreviewBackground.setBackgroundColor(nPixelColor); @@ -152,7 +152,7 @@ public class MainViewFragment extends Fragment { @Override public void onResume() { super.onResume(); - BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity()); + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(getActivity()); BackgroundPictureBean bean = utils.getBackgroundPictureBean(); int nPixelColor = bean.getPixelColor(); bvPreviewBackground.setBackgroundColor(nPixelColor); diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/utils/BackgroundPictureUtils.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/utils/BackgroundSourceUtils.java similarity index 85% rename from powerbell/src/main/java/cc/winboll/studio/powerbell/utils/BackgroundPictureUtils.java rename to powerbell/src/main/java/cc/winboll/studio/powerbell/utils/BackgroundSourceUtils.java index 055f9d13..3b18f00e 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/utils/BackgroundPictureUtils.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/utils/BackgroundSourceUtils.java @@ -9,26 +9,26 @@ import android.content.Context; import cc.winboll.studio.powerbell.beans.BackgroundPictureBean; import java.io.File; -public class BackgroundPictureUtils { +public class BackgroundSourceUtils { public static final String TAG = "BackgroundPictureUtils"; - static BackgroundPictureUtils _mBackgroundPictureUtils; + static BackgroundSourceUtils _mBackgroundPictureUtils; Context mContext; BackgroundPictureBean mBackgroundPictureBean; // 背景图片目录 String mszBackgroundDir; - BackgroundPictureUtils(Context context) { + BackgroundSourceUtils(Context context) { mContext = context; String szExternalFilesDir = mContext.getExternalFilesDir(TAG) + File.separator; setBackgroundDir(szExternalFilesDir + "Background" + File.separator); loadBackgroundPictureBean(); } - public static BackgroundPictureUtils getInstance(Context context) { + public static BackgroundSourceUtils getInstance(Context context) { if (_mBackgroundPictureUtils == null) { - _mBackgroundPictureUtils = new BackgroundPictureUtils(context); + _mBackgroundPictureUtils = new BackgroundSourceUtils(context); } return _mBackgroundPictureUtils; } diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/utils/PictureUtils.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/utils/PictureUtils.java deleted file mode 100644 index 17e0c3df..00000000 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/utils/PictureUtils.java +++ /dev/null @@ -1,207 +0,0 @@ -package cc.winboll.studio.powerbell.utils; - -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.Environment; -import android.util.Log; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -/** - * @Author ZhanGSKen&豆包大模型 - * @Date 2025/11/21 18:55 - * @Describe - * 图片下载工具类(指定目录保存:Pictures/PowerBell/BackgroundHistory) - */ -public class PictureUtils { - private static final String TAG = "PictureUtils"; - private static final String ROOT_DIR = "PowerBell/BackgroundHistory"; // 自定义目录结构 - private static OkHttpClient sOkHttpClient; - - static { - sOkHttpClient = new OkHttpClient(); - } - - /** - * 下载网络图片到指定目录(外部存储/Pictures/PowerBell/BackgroundHistory) - * @param context 上下文(用于通知相册刷新) - * @param imgUrl 图片网络URL - * @param callback 下载结果回调(成功/失败) - */ - public static void downloadImageToAlbum(final Context context, final String imgUrl, final DownloadCallback callback) { - // 检查参数合法性 - if (context == null) { - if (callback != null) { - callback.onFailure(new IllegalArgumentException("Context不能为空")); - } - return; - } - if (imgUrl == null || imgUrl.isEmpty()) { - if (callback != null) { - callback.onFailure(new IllegalArgumentException("图片URL为空")); - } - return; - } - - startDownload(context, imgUrl, callback); - } - - /** - * 执行实际的下载逻辑 - */ - private static void startDownload(final Context context, final String imgUrl, final DownloadCallback callback) { - Request request = new Request.Builder().url(imgUrl).build(); - sOkHttpClient.newCall(request).enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) throws IOException { - if (!response.isSuccessful()) { - if (callback != null) { - callback.onFailure(new IOException("请求失败,响应码:" + response.code())); - } - return; - } - - InputStream inputStream = null; - FileOutputStream outputStream = null; - try { - inputStream = response.body().byteStream(); - // 1. 获取并创建指定保存目录(外部存储/Pictures/PowerBell/BackgroundHistory) - File saveDir = getTargetSaveDir(context); - if (!saveDir.exists()) { - boolean isDirCreated = saveDir.mkdirs(); // 递归创建多级目录 - if (!isDirCreated) { - if (callback != null) { - callback.onFailure(new IOException("创建目录失败:" + saveDir.getAbsolutePath())); - } - return; - } - } - - // 2. 解析图片后缀 - String fileSuffix = getImageSuffix(imgUrl, response); - // 3. 生成时间戳文件名 - String fileName = generateTimeFileName() + fileSuffix; - // 4. 创建文件 - final File saveFile = new File(saveDir, fileName); - - // 5. 写入文件 - outputStream = new FileOutputStream(saveFile); - byte[] buffer = new byte[1024 * 4]; - int len; - while ((len = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, len); - } - outputStream.flush(); - - // 6. 通知相册刷新(使图片显示在系统相册中) - notifyAlbumRefresh(context, saveFile); - - // 成功回调 - if (callback != null) { - callback.onSuccess(saveFile.getAbsolutePath()); - } - - } catch (Exception e) { - Log.e(TAG, "下载图片异常", e); - if (callback != null) { - callback.onFailure(e); - } - } finally { - // 关闭资源 - if (inputStream != null) inputStream.close(); - if (outputStream != null) outputStream.close(); - if (response.body() != null) response.body().close(); - } - } - - @Override - public void onFailure(Call call, final IOException e) { - Log.e(TAG, "下载图片失败", e); - if (callback != null) { - callback.onFailure(e); - } - } - }); - } - - /** - * 获取目标保存目录:外部存储/Pictures/PowerBell/BackgroundHistory - */ - private static File getTargetSaveDir(Context context) { - // 优先使用公共Pictures目录(兼容多数设备) - File publicPicturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); - if (publicPicturesDir.exists()) { - return new File(publicPicturesDir, ROOT_DIR); - } - // 备选:应用私有Pictures目录(若公共目录不可用) - File appPicturesDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); - if (appPicturesDir != null) { - return new File(appPicturesDir, ROOT_DIR); - } - // 极端情况:外部存储根目录 - return new File(Environment.getExternalStorageDirectory(), ROOT_DIR); - } - - /** - * 解析图片后缀名 - */ - private static String getImageSuffix(String imgUrl, Response response) { - // 优先从URL解析 - if (imgUrl.lastIndexOf('.') != -1) { - String suffix = imgUrl.substring(imgUrl.lastIndexOf('.')); - if (suffix.length() <= 5 && (suffix.contains("png") || suffix.contains("jpg") || suffix.contains("jpeg") || suffix.contains("gif"))) { - return suffix.toLowerCase(Locale.getDefault()); - } - } - // 从响应头解析 - String contentType = response.header("Content-Type"); - if (contentType != null) { - if (contentType.contains("png")) return ".png"; - if (contentType.contains("jpeg") || contentType.contains("jpg")) return ".jpg"; - if (contentType.contains("gif")) return ".gif"; - } - return ".jpg"; // 默认后缀 - } - - /** - * 生成时间戳文件名 - */ - private static String generateTimeFileName() { - SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.getDefault()); - return sdf.format(new Date()); - } - - /** - * 通知相册刷新 - */ - private static void notifyAlbumRefresh(Context context, File file) { - try { - Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); - Uri uri = Uri.fromFile(file); - intent.setData(uri); - context.sendBroadcast(intent); - } catch (Exception e) { - Log.e(TAG, "通知相册刷新失败", e); - } - } - - /** - * 下载结果回调接口 - */ - public interface DownloadCallback { - void onSuccess(String savePath); // 下载成功(子线程回调) - void onFailure(Exception e); // 下载失败(子线程回调) - } -} -