Compare commits
8 Commits
powerbell-
...
positions-
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b54a6ec0f | |||
| bdb94b5c18 | |||
| 7563ea01f9 | |||
| 13b171915e | |||
| d38407099d | |||
| af8d2b9d52 | |||
| d7a6d8d4a3 | |||
| c725576d58 |
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools" >
|
||||
|
||||
<application>
|
||||
|
||||
<!-- Put flavor specific code here -->
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
Built-By: Generated-by-ADT
|
||||
Created-By: Android Gradle 3.5.0
|
||||
|
||||
Name: AndroidManifest.xml
|
||||
SHA1-Digest: U36A0NWthb49+Rxs33tkIuNYFCI=
|
||||
|
||||
Name: jni/Android.mk
|
||||
SHA1-Digest: ZpGSlRJPL0g9OejiWbQorqj40/Y=
|
||||
|
||||
Name: jni/Application.mk
|
||||
SHA1-Digest: TKh2CbRLeKfvgL4cPfmoxcVz+vc=
|
||||
|
||||
Name: jni/hello-jni.cpp
|
||||
SHA1-Digest: 1btXO19SqB6rDvPo5ynG0brDqTk=
|
||||
|
||||
Name: project.properties
|
||||
SHA1-Digest: 0ekOiGTFMVJOWqAFzNFj/1vxPL8=
|
||||
|
||||
Name: res/values/strings.xml
|
||||
SHA1-Digest: FgRO/zbNaC1wuZKVT7h6NSYBmpY=
|
||||
|
||||
Name: src/$package_name$/HelloJni.java
|
||||
SHA1-Digest: p0e9DNKocjRnsOhetb9bnp9s9J4=
|
||||
|
||||
|
||||
@@ -32,18 +32,12 @@ android {
|
||||
// versionName 更新后需要手动设置
|
||||
// .winboll/winbollBuildProps.properties 文件的 stageCount=0
|
||||
// Gradle编译环境下合起来的 versionName 就是 "${versionName}.0"
|
||||
versionName "15.11"
|
||||
versionName "15.12"
|
||||
if(true) {
|
||||
versionName = genVersionName("${versionName}")
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -72,7 +66,14 @@ dependencies {
|
||||
//api 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
|
||||
//api 'androidx.fragment:fragment:1.1.0'
|
||||
|
||||
api 'cc.winboll.studio:libaes:15.11.0'
|
||||
api 'cc.winboll.studio:libappbase:15.11.0'
|
||||
|
||||
// WinBoLL库 nexus.winboll.cc 地址
|
||||
//api 'cc.winboll.studio:libaes:15.12.0'
|
||||
//api 'cc.winboll.studio:libappbase:15.12.2'
|
||||
|
||||
// WinBoLL备用库 jitpack.io 地址
|
||||
api 'com.github.ZhanGSKen:AES:aes-v15.12.3'
|
||||
api 'com.github.ZhanGSKen:APPBase:appbase-v15.12.2'
|
||||
|
||||
api fileTree(dir: 'libs', include: ['*.jar'])
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Sat Nov 15 08:45:48 GMT 2025
|
||||
stageCount=2
|
||||
#Sun Dec 07 21:00:59 HKT 2025
|
||||
stageCount=1
|
||||
libraryProject=
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.1
|
||||
buildCount=21
|
||||
baseBetaVersion=15.11.2
|
||||
baseVersion=15.12
|
||||
publishVersion=15.12.0
|
||||
buildCount=0
|
||||
baseBetaVersion=15.12.1
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="cc.winboll.studio.positions">
|
||||
|
||||
<!-- 只能在前台获取精确的位置信息 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
<!-- 只有在前台运行时才能获取大致位置信息 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||
|
||||
@@ -30,6 +27,9 @@
|
||||
<!-- 修改或删除您共享存储空间中的内容 -->
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
|
||||
<!-- 只能在前台获取精确的位置信息 -->
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
<uses-feature
|
||||
android:name="android.hardware.location.gps"
|
||||
android:required="false"/>
|
||||
@@ -65,8 +65,6 @@
|
||||
|
||||
</activity>
|
||||
|
||||
<activity android:name=".activities.CrashActivity"/>
|
||||
|
||||
<activity-alias
|
||||
android:name=".MainActivityWukong"
|
||||
android:targetActivity=".MainActivity"
|
||||
@@ -111,16 +109,10 @@
|
||||
|
||||
</activity-alias>
|
||||
|
||||
<meta-data
|
||||
android:name="android.max_aspect"
|
||||
android:value="4.0"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.positions.activities.LocationActivity"/>
|
||||
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.version"
|
||||
android:value="@integer/google_play_services_version"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.positions.activities.ShortcutActionActivity"/>
|
||||
|
||||
<service
|
||||
android:name=".services.MainService"
|
||||
android:exported="false"/>
|
||||
@@ -142,6 +134,15 @@
|
||||
</intent-filter>
|
||||
|
||||
</receiver>
|
||||
|
||||
<meta-data
|
||||
android:name="android.max_aspect"
|
||||
android:value="4.0"/>
|
||||
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.version"
|
||||
android:value="@integer/google_play_services_version"/>
|
||||
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
@@ -151,12 +152,10 @@
|
||||
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths"/>
|
||||
android:resource="@xml/file_provider"/>
|
||||
|
||||
</provider>
|
||||
|
||||
<activity android:name="cc.winboll.studio.positions.activities.ShortcutActionActivity"/>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
</manifest>
|
||||
|
||||
@@ -14,6 +14,7 @@ import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.ViewGroup;
|
||||
@@ -24,7 +25,6 @@ import android.widget.Toast;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.utils.MyActivityLifecycleCallbacks;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
@@ -44,36 +44,24 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class App extends GlobalApplication {
|
||||
|
||||
public static volatile AppLevel _mAppLevel = AppLevel.WUKONG;
|
||||
|
||||
public static final String COMPONENT_WUKONG = "cc.winboll.studio.positions.MainActivityWukong";
|
||||
public static final String COMPONENT_LAOJUN = "cc.winboll.studio.positions.MainActivityLaojun";
|
||||
public static final String ACTION_OPEN_APPPLUS = "cc.winboll.studio.positions.App.ACTION_OPEN_APPPLUS";
|
||||
public static final String ACTION_CLOSE_APPPLUS = "cc.winboll.studio.positions.App.ACTION_CLOSE_APPPLUS";
|
||||
|
||||
private static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
|
||||
|
||||
MyActivityLifecycleCallbacks mMyActivityLifecycleCallbacks;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
setIsDebugging(BuildConfig.DEBUG);
|
||||
|
||||
|
||||
WinBoLLActivityManager.init(this);
|
||||
|
||||
|
||||
// 初始化 Toast 框架
|
||||
ToastUtils.init(this);
|
||||
// 设置 Toast 布局样式
|
||||
//ToastUtils.setView(R.layout.view_toast);
|
||||
//ToastUtils.setStyle(new WhiteToastStyle());
|
||||
//ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
|
||||
|
||||
|
||||
//CrashHandler.getInstance().registerGlobal(this);
|
||||
//CrashHandler.getInstance().registerPart(this);
|
||||
mMyActivityLifecycleCallbacks = new MyActivityLifecycleCallbacks();
|
||||
registerActivityLifecycleCallbacks(mMyActivityLifecycleCallbacks);
|
||||
}
|
||||
|
||||
public static void write(InputStream input, OutputStream output) throws IOException {
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package cc.winboll.studio.positions;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/10 07:23
|
||||
* @Describe 应用级别类型枚举
|
||||
*/
|
||||
public enum AppLevel {
|
||||
WUKONG("wukong", "悟空级别"),
|
||||
LAOJUN("laojun", "老君级别");
|
||||
|
||||
public static final String TAG = "AppLevel";
|
||||
|
||||
// 枚举属性
|
||||
private final String code; // 编码(如 "wukong")
|
||||
private final String desc; // 描述
|
||||
|
||||
// 构造方法(Java 7 需显式定义)
|
||||
AppLevel(String code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
// Getter 方法(获取枚举属性)
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
// 可选:根据 code 获取枚举项(便于业务使用)
|
||||
public static AppLevel getByCode(String code) {
|
||||
for (AppLevel level : values()) {
|
||||
if (level.code.equals(code)) {
|
||||
return level;
|
||||
}
|
||||
}
|
||||
return null; // 或抛出异常,根据业务需求调整
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.CompoundButton;
|
||||
@@ -18,12 +17,10 @@ import androidx.core.content.ContextCompat;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.activities.LocationActivity;
|
||||
import cc.winboll.studio.positions.activities.WinBoLLActivity;
|
||||
import cc.winboll.studio.positions.utils.APPPlusUtils;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import cc.winboll.studio.positions.utils.AppConfigsUtil;
|
||||
import cc.winboll.studio.positions.utils.JsonShareHandler;
|
||||
import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||
|
||||
/**
|
||||
@@ -34,18 +31,17 @@ import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||
*/
|
||||
public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
public static final String TAG = "MainActivity";
|
||||
|
||||
// 权限请求码(建议定义为类常量,避免魔法值)
|
||||
// 权限请求码(建议定义为类常量,避免魔法值)
|
||||
private static final int REQUEST_LOCATION_PERMISSIONS = 1001;
|
||||
private static final int REQUEST_BACKGROUND_LOCATION_PERMISSION = 1002;
|
||||
|
||||
|
||||
// UI 控件:服务控制开关、顶部工具栏
|
||||
private Switch mServiceSwitch;
|
||||
private Button mManagePositionsButton;
|
||||
private Toolbar mToolbar;
|
||||
// 服务相关:服务实例、绑定状态标记
|
||||
//private DistanceRefreshService mDistanceService;
|
||||
//private boolean isServiceBound = false;
|
||||
private boolean isServiceBound = false;
|
||||
|
||||
|
||||
@Override
|
||||
@@ -87,10 +83,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main); // 关联主页面布局
|
||||
|
||||
// 处理启动时的分享 Intent
|
||||
handleShareIntent(getIntent());
|
||||
|
||||
|
||||
// 1. 初始化顶部 Toolbar(保留原逻辑,设置页面标题)
|
||||
initToolbar();
|
||||
// 2. 初始化其他控件
|
||||
@@ -103,31 +95,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
//bindDistanceService();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
// 处理后续接收的分享 Intent(如应用已在后台)
|
||||
handleShareIntent(intent);
|
||||
}
|
||||
|
||||
private void handleShareIntent(Intent intent) {
|
||||
if (intent != null && Intent.ACTION_SEND.equals(intent.getAction())) {
|
||||
// 调用工具类,弹出确认对话框
|
||||
JsonShareHandler.handleSharedJsonWithConfirm(this, intent, new JsonShareHandler.ConfirmCallback() {
|
||||
@Override
|
||||
public void onConfirm(boolean isConfirm) {
|
||||
// 回调处理:isConfirm 为 true 表示接收并保存,false 表示取消
|
||||
if (!isConfirm) {
|
||||
Log.d("MainActivity", "用户取消接收文件");
|
||||
// 可添加取消后的逻辑(如关闭页面)
|
||||
// finish();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
@@ -147,9 +114,9 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
mToolbar = (Toolbar) findViewById(R.id.toolbar); // Java 7 显式 findViewById + 强转
|
||||
setSupportActionBar(mToolbar);
|
||||
// 给ActionBar设置标题(先判断非空,避免空指针异常)
|
||||
AppLevel appLevel = AppConfigsUtil.getInstance(getApplicationContext()).getAppLevel(true);
|
||||
getSupportActionBar().setTitle(getString(R.string.app_name));
|
||||
|
||||
if (getSupportActionBar() != null) {
|
||||
getSupportActionBar().setTitle(getString(R.string.app_name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package cc.winboll.studio.positions;
|
||||
import android.os.Bundle;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/13 15:21
|
||||
* @Describe MainActivityLaojun
|
||||
*/
|
||||
public class MainActivityLaojun extends MainActivity {
|
||||
|
||||
public static final String TAG = "MainActivityLaojun";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
ToastUtils.show("道法自然");
|
||||
LogUtils.d(TAG, "玩法归臻");
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package cc.winboll.studio.positions;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/15 15:14
|
||||
* @Describe 应用入口级别类型枚举
|
||||
*/
|
||||
public enum PointLevel {
|
||||
DORAEMON("doraemon", "叮铛级别"),
|
||||
WUKONG("wukong", "悟空级别"),
|
||||
LAOJUN("laojun", "老君级别");
|
||||
|
||||
public static final String TAG = "PointLevel";
|
||||
|
||||
// 枚举属性
|
||||
private final String code; // 编码(如 "wukong")
|
||||
private final String desc; // 描述
|
||||
|
||||
// 构造方法(Java 7 需显式定义)
|
||||
PointLevel(String code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
// Getter 方法(获取枚举属性)
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
// 可选:根据 code 获取枚举项(便于业务使用)
|
||||
public static PointLevel getByCode(String code) {
|
||||
for (PointLevel level : values()) {
|
||||
if (level.code.equals(code)) {
|
||||
return level;
|
||||
}
|
||||
}
|
||||
return null; // 或抛出异常,根据业务需求调整
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package cc.winboll.studio.positions.activities;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.PersistableBundle;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.R;
|
||||
import cc.winboll.studio.positions.utils.APPPlusUtils;
|
||||
import cc.winboll.studio.positions.utils.AppConfigsUtil;
|
||||
import cc.winboll.studio.positions.AppLevel;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/15 13:45
|
||||
* @Describe 应用快捷方式活动类
|
||||
*/
|
||||
public class ShortcutActionActivity extends Activity {
|
||||
|
||||
public static final String TAG = "ShortcutActionActivity";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// 处理应用级别的切换请求
|
||||
handleSwitchRequest();
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// @Override
|
||||
// public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
|
||||
// super.onPostCreate(savedInstanceState, persistentState);
|
||||
// finish();
|
||||
// }
|
||||
|
||||
// @Override
|
||||
// protected void onStart() {
|
||||
// super.onStart();
|
||||
// }
|
||||
|
||||
/**
|
||||
* 处理应用图标快捷菜单的请求
|
||||
*/
|
||||
private void handleSwitchRequest() {
|
||||
Intent intent = getIntent();
|
||||
if (intent != null && "open_appplus".equals(intent.getDataString())) {
|
||||
ToastUtils.show("已添加" + getString(R.string.app_name) + "附加组件");
|
||||
AppConfigsUtil.getInstance(getApplicationContext()).setAppLevel(AppLevel.LAOJUN);
|
||||
APPPlusUtils.openAPPPlus(this);
|
||||
//moveTaskToBack(true);
|
||||
}
|
||||
if (intent != null && "close_appplus".equals(intent.getDataString())) {
|
||||
ToastUtils.show("已移除" + getString(R.string.app_name) + "附加组件");
|
||||
AppConfigsUtil.getInstance(getApplicationContext()).setAppLevel(AppLevel.WUKONG);
|
||||
APPPlusUtils.closeAPPPlus(this);
|
||||
//moveTaskToBack(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,19 +12,11 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.App;
|
||||
import cc.winboll.studio.positions.PointLevel;
|
||||
import cc.winboll.studio.positions.R;
|
||||
import cc.winboll.studio.positions.utils.ActivityAliasUtils;
|
||||
import cc.winboll.studio.positions.utils.AppConfigsUtil;
|
||||
|
||||
public class WinBoLLActivity extends AppCompatActivity implements IWinBoLLActivity {
|
||||
|
||||
public static final String TAG = "WinBoLLActivity";
|
||||
|
||||
public static volatile PointLevel _mPointLevel = PointLevel.WUKONG;
|
||||
|
||||
@Override
|
||||
public Activity getActivity() {
|
||||
return this;
|
||||
@@ -38,67 +30,18 @@ public class WinBoLLActivity extends AppCompatActivity implements IWinBoLLActivi
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
//ToastUtils.show("onResume");
|
||||
|
||||
// ActivityAliasUtils 工具使用示例
|
||||
//
|
||||
// // 获取真实的目标组件名(即使通过 alias 启动,也能拿到 OriginalActivity)
|
||||
// String realTargetName = ActivityAliasUtils.getRealTargetNameFromIntent(this);
|
||||
// LogUtils.d("AliasActivity", "真实组件名:" + realTargetName);
|
||||
// 获取真实的目标组件名(即使通过 alias 启动,也能拿到 OriginalActivity)
|
||||
// String realTargetName = ActivityAliasUtils.getRealTargetNameFromIntent(this);
|
||||
// LogUtils.d(TAG, "真实组件名:" + realTargetName);
|
||||
// ToastUtils.show(realTargetName);
|
||||
// // 判断某个组件是否为 alias
|
||||
// String componentName = "com.winboll.app.AliasActivity";
|
||||
// boolean isAlias = ActivityAliasUtils.isActivityAlias(getApplicationContext(), componentName);
|
||||
// LogUtils.d("判断结果", componentName + " 是否为 alias:" + isAlias); // true
|
||||
// // 获取启动当前 Activity 的组件名(兼容 alias 场景)
|
||||
// String launchComponent = ActivityAliasUtils.getLaunchComponentName(this);
|
||||
// LogUtils.d("MainActivity", "启动组件名:" + launchComponent);
|
||||
|
||||
/*
|
||||
* 应用入口逻辑模块
|
||||
*/
|
||||
//
|
||||
// 检查当前活动的启动组件名,设置应用入口级别。
|
||||
String launchComponent = ActivityAliasUtils.getLaunchComponentName(this);
|
||||
LogUtils.d("MainActivity", "启动组件名:" + launchComponent);
|
||||
ToastUtils.show(launchComponent);
|
||||
// 当前应用处于活动暂停的状态时,就检查应用的入口组件名称,设置应用入口级别。
|
||||
if (WinBoLLActivity._mPointLevel == PointLevel.DORAEMON) {
|
||||
if (launchComponent.equals(App.COMPONENT_WUKONG)) {
|
||||
getSupportActionBar().setTitle(getString(R.string.appplus_name));
|
||||
ToastUtils.show("WUKONG");
|
||||
_mPointLevel = PointLevel.WUKONG;
|
||||
} else if (launchComponent.equals(App.COMPONENT_LAOJUN)) {
|
||||
getSupportActionBar().setTitle(getString(R.string.app_name));
|
||||
ToastUtils.show("LAOJUN");
|
||||
_mPointLevel = PointLevel.LAOJUN;
|
||||
} else {
|
||||
// 如果是其他应用组件入口,就关闭活动
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 应用级别设置模块
|
||||
*/
|
||||
// 读取并配置应用级别
|
||||
App._mAppLevel = AppConfigsUtil.getInstance(getApplicationContext()).getAppLevel(true);
|
||||
|
||||
LogUtils.d(TAG, String.format("onResume %s", getTag()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
/*if (item.getItemId() == R.id.item_log) {
|
||||
WinBoLLActivityManager.getInstance().startLogActivity(this);
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.item_home) {
|
||||
startActivity(new Intent(this, MainActivity.class));
|
||||
return true;
|
||||
}*/
|
||||
WinBoLLActivityManager.getInstance().startLogActivity(this);
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.item_home) {
|
||||
startActivity(new Intent(this, MainActivity.class));
|
||||
return true;
|
||||
}*/
|
||||
// 在switch语句中处理每个ID,并在处理完后返回true,未处理的情况返回false。
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@@ -9,14 +9,12 @@ package cc.winboll.studio.positions.models;
|
||||
import android.util.JsonWriter;
|
||||
import android.util.JsonReader;
|
||||
import java.io.IOException;
|
||||
import cc.winboll.studio.positions.AppLevel;
|
||||
|
||||
public class AppConfigsModel extends BaseBean {
|
||||
|
||||
public static final String TAG = "AppConfigsModel";
|
||||
|
||||
boolean isEnableMainService;
|
||||
AppLevel appLevel;
|
||||
|
||||
public AppConfigsModel(boolean isEnableMainService) {
|
||||
this.isEnableMainService = isEnableMainService;
|
||||
@@ -26,14 +24,6 @@ public class AppConfigsModel extends BaseBean {
|
||||
this.isEnableMainService = false;
|
||||
}
|
||||
|
||||
public void setAppLevel(AppLevel appLevel) {
|
||||
this.appLevel = appLevel;
|
||||
}
|
||||
|
||||
public AppLevel getAppLevel() {
|
||||
return appLevel;
|
||||
}
|
||||
|
||||
public void setIsEnableMainService(boolean isEnableMainService) {
|
||||
this.isEnableMainService = isEnableMainService;
|
||||
}
|
||||
@@ -52,7 +42,6 @@ public class AppConfigsModel extends BaseBean {
|
||||
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
|
||||
super.writeThisToJsonWriter(jsonWriter);
|
||||
jsonWriter.name("isEnableDistanceRefreshService").value(isEnableMainService());
|
||||
jsonWriter.name("appLevel").value(getAppLevel().ordinal());
|
||||
}
|
||||
|
||||
// JSON反序列化(加载位置数据,校验字段)
|
||||
@@ -63,8 +52,6 @@ public class AppConfigsModel extends BaseBean {
|
||||
} else {
|
||||
if (name.equals("isEnableDistanceRefreshService")) {
|
||||
setIsEnableMainService(jsonReader.nextBoolean());
|
||||
} else if (name.equals("appLevel")) {
|
||||
setAppLevel((AppLevel.values()[jsonReader.nextInt()]));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,361 +0,0 @@
|
||||
package cc.winboll.studio.positions.receivers;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/10/28 19:07
|
||||
* @Describe MotionStatusReceiver
|
||||
*/
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.services.MainService;
|
||||
import cc.winboll.studio.positions.utils.ServiceUtil;
|
||||
|
||||
/**
|
||||
* 运动状态监听Receiver
|
||||
* 功能:1.持续监听传感器(不关闭) 2.每5秒计算运动状态 3.按状态切换GPS模式(实时/30秒定时)
|
||||
*/
|
||||
public class MotionStatusReceiver extends BroadcastReceiver implements SensorEventListener {
|
||||
public static final String TAG = "MotionStatusReceiver";
|
||||
|
||||
// 广播Action
|
||||
public static final String ACTION_MOTION_STATUS_RECEIVER = "cc.winboll.studio.positions.receivers.MotionStatusReceiver";
|
||||
public static final String EXTRA_SENSORS_ENABLE = "EXTRA_SENSORS_ENABLE";
|
||||
// 传感器启动状态标志位
|
||||
boolean mIsSensorsEnable = false;
|
||||
// 运动状态常量
|
||||
private static final int MOTION_STATUS_STATIC = 0; // 静止/低运动
|
||||
private static final int MOTION_STATUS_WALKING = 1; // 行走/高速运动
|
||||
// 配置参数(按需求调整)
|
||||
private static final float ACCELEROMETER_THRESHOLD = 0.8f; // 加速度阈值
|
||||
private static final float GYROSCOPE_THRESHOLD = 0.5f; // 陀螺仪阈值
|
||||
private static final long STATUS_CALC_INTERVAL = 5000; // 运动状态计算间隔(5秒)
|
||||
private static final long GPS_STATIC_INTERVAL = 30; // 静止时GPS间隔(30秒)
|
||||
// 核心对象
|
||||
private volatile SensorManager mSensorManager;
|
||||
private Sensor mAccelerometer;
|
||||
private Sensor mGyroscope;
|
||||
private volatile boolean mIsSensorListening = false; // 传感器是否持续监听
|
||||
private int mCurrentMotionStatus = MOTION_STATUS_STATIC; // 当前运动状态
|
||||
private Handler mMainHandler; // 主线程Handler(用于定时计算)
|
||||
private Context mBroadcastContext; // 广播上下文
|
||||
// 传感器数据缓存(用于5秒内数据汇总,避免单次波动误判)
|
||||
private float mAccelMax = 0f; // 5秒内加速度最大值
|
||||
private float mGyroMax = 0f; // 5秒内陀螺仪最大值
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
LogUtils.d(TAG, "===== 接收器启动:onReceive() 开始执行 =====");
|
||||
this.mBroadcastContext = context;
|
||||
mMainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
|
||||
boolean isSettingEnable = intent.getBooleanExtra(EXTRA_SENSORS_ENABLE, false);
|
||||
if (mIsSensorsEnable == false && isSettingEnable == true) {
|
||||
mIsSensorsEnable = true;
|
||||
// 1. 初始化传感器(必执行)
|
||||
initSensors();
|
||||
|
||||
|
||||
if (mAccelerometer == null || mGyroscope == null) {
|
||||
LogUtils.e(TAG, "设备缺少加速度/陀螺仪,无法持续监听");
|
||||
cleanResources(false); // 传感器不可用才清理
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 校验参数
|
||||
if (context == null || intent == null) {
|
||||
LogUtils.d(TAG, "onReceive():无效参数,终止处理");
|
||||
cleanResources(false);
|
||||
return;
|
||||
}
|
||||
LogUtils.d(TAG, "onReceive():接收到广播Action=" + intent.getAction());
|
||||
|
||||
// 3. 启动持续传感器监听(核心:不关闭,重复调用无影响)
|
||||
startSensorListening();
|
||||
|
||||
// 4. 启动5秒定时计算运动状态(核心:持续触发状态判断)
|
||||
startStatusCalcTimer();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 5. 处理外部广播触发(可选,保留外部控制能力)
|
||||
// if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
|
||||
// int motionStatus = intent.getIntExtra(EXTRA_MOTION_STATUS, MOTION_STATUS_STATIC);
|
||||
// String statusDesc = motionStatus == MOTION_STATUS_WALKING ? "高速运动" : "静止/低运动";
|
||||
// LogUtils.d(TAG, "外部广播触发,强制设置运动状态:" + statusDesc);
|
||||
// mCurrentMotionStatus = motionStatus;
|
||||
// handleMotionStatus(mCurrentMotionStatus); // 立即执行GPS切换
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化传感器(持续监听,复用实例)
|
||||
*/
|
||||
private void initSensors() {
|
||||
LogUtils.d(TAG, "initSensors():初始化传感器");
|
||||
if (mSensorManager != null || mBroadcastContext == null) return;
|
||||
|
||||
mSensorManager = (SensorManager) mBroadcastContext.getSystemService(Context.SENSOR_SERVICE);
|
||||
if (mSensorManager == null) {
|
||||
LogUtils.e(TAG, "设备不支持传感器服务");
|
||||
return;
|
||||
}
|
||||
// 获取传感器实例(持续复用,不销毁)
|
||||
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
|
||||
|
||||
LogUtils.d(TAG, "传感器初始化结果:加速度=" + (mAccelerometer != null) + ",陀螺仪=" + (mGyroscope != null));
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动传感器持续监听(核心:不关闭,注册一次一直生效)
|
||||
*/
|
||||
private void startSensorListening() {
|
||||
if (mSensorManager == null || mAccelerometer == null || mGyroscope == null) return;
|
||||
|
||||
if (!mIsSensorListening) {
|
||||
// 注册传感器监听(持续生效,直到服务销毁才注销)
|
||||
mSensorManager.registerListener(
|
||||
this,
|
||||
mAccelerometer,
|
||||
SensorManager.SENSOR_DELAY_NORMAL, // 正常延迟,平衡性能与精度
|
||||
mMainHandler
|
||||
);
|
||||
mSensorManager.registerListener(
|
||||
this,
|
||||
mGyroscope,
|
||||
SensorManager.SENSOR_DELAY_NORMAL,
|
||||
mMainHandler
|
||||
);
|
||||
mIsSensorListening = true;
|
||||
LogUtils.d(TAG, "startSensorListening():传感器持续监听已启动(不关闭)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动5秒定时计算运动状态(核心:周期性汇总传感器数据)
|
||||
*/
|
||||
private void startStatusCalcTimer() {
|
||||
if (mMainHandler == null) return;
|
||||
|
||||
// 移除旧任务(避免重复注册)
|
||||
mMainHandler.removeCallbacks(mStatusCalcRunnable);
|
||||
// 启动定时任务(每5秒执行一次)
|
||||
mMainHandler.postDelayed(mStatusCalcRunnable, STATUS_CALC_INTERVAL);
|
||||
LogUtils.d(TAG, "startStatusCalcTimer():5秒运动状态计算定时器已启动");
|
||||
}
|
||||
|
||||
/**
|
||||
* 运动状态计算任务(5秒执行一次)
|
||||
*/
|
||||
private final Runnable mStatusCalcRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 1. 基于5秒内缓存的最大传感器数据判断状态
|
||||
boolean isHighMotion = (mAccelMax > ACCELEROMETER_THRESHOLD) && (mGyroMax > GYROSCOPE_THRESHOLD);
|
||||
int newMotionStatus = isHighMotion ? MOTION_STATUS_WALKING : MOTION_STATUS_STATIC;
|
||||
|
||||
// 2. 状态变化时才处理(避免频繁切换GPS)
|
||||
if (newMotionStatus != mCurrentMotionStatus) {
|
||||
mCurrentMotionStatus = newMotionStatus;
|
||||
String statusDesc = isHighMotion ? "高速运动" : "静止/低运动";
|
||||
LogUtils.d(TAG, "运动状态更新(5秒计算):" + statusDesc
|
||||
+ "(加速度最大值=" + mAccelMax + ",陀螺仪最大值=" + mGyroMax + ")");
|
||||
handleMotionStatus(newMotionStatus); // 切换GPS模式
|
||||
} else {
|
||||
LogUtils.d(TAG, "运动状态无变化(5秒计算):" + (isHighMotion ? "高速运动" : "静止/低运动"));
|
||||
}
|
||||
|
||||
// 3. 重置传感器数据缓存,准备下一个5秒周期
|
||||
mAccelMax = 0f;
|
||||
mGyroMax = 0f;
|
||||
|
||||
// 4. 循环执行定时任务(核心:持续计算)
|
||||
mMainHandler.postDelayed(this, STATUS_CALC_INTERVAL);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 传感器数据变化回调(核心:实时缓存最大数据)
|
||||
*/
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if (event == null) return;
|
||||
|
||||
// 实时缓存5秒内的最大传感器数据(避免单次波动误判)
|
||||
switch (event.sensor.getType()) {
|
||||
case Sensor.TYPE_ACCELEROMETER:
|
||||
float accelTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
|
||||
if (accelTotal > mAccelMax) mAccelMax = accelTotal; // 缓存最大值
|
||||
LogUtils.d(TAG, "加速度传感器实时数据:合值=" + accelTotal + "(当前5秒最大值=" + mAccelMax + ")");
|
||||
break;
|
||||
case Sensor.TYPE_GYROSCOPE:
|
||||
float gyroTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
|
||||
if (gyroTotal > mGyroMax) mGyroMax = gyroTotal; // 缓存最大值
|
||||
LogUtils.d(TAG, "陀螺仪实时数据:合值=" + gyroTotal + "(当前5秒最大值=" + mGyroMax + ")");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理运动状态(核心:按状态切换GPS模式)
|
||||
*/
|
||||
private void handleMotionStatus(int motionStatus) {
|
||||
LogUtils.d(TAG, "handleMotionStatus():开始处理运动状态,切换GPS模式");
|
||||
if (mBroadcastContext == null) {
|
||||
LogUtils.w(TAG, "上下文为空,无法处理GPS");
|
||||
return;
|
||||
}
|
||||
|
||||
MainService mainService = getMainService();
|
||||
if (mainService == null) {
|
||||
LogUtils.e(TAG, "MainService未启动,GPS控制失败");
|
||||
return;
|
||||
}
|
||||
|
||||
if (motionStatus == MOTION_STATUS_WALKING) {
|
||||
// 高速运动:启动GPS实时更新(2秒/1米)
|
||||
handleHighMotionGPS(mainService);
|
||||
} else {
|
||||
// 静止/低运动:启动GPS30秒定时更新
|
||||
handleStaticGPS(mainService);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 高速运动GPS处理:实时更新
|
||||
*/
|
||||
private void handleHighMotionGPS(MainService mainService) {
|
||||
// 动态权限判断(Android 6.0+)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
||||
mBroadcastContext.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
sendPermissionRequestBroadcast();
|
||||
return;
|
||||
}
|
||||
|
||||
// 启动实时GPS(已启动则不重复操作)
|
||||
if (!mainService.isGpsListening()) {
|
||||
mainService.startGpsLocation(); // 实时更新(2秒/1米)
|
||||
mainService.stopGpsStaticTimer(); // 停止定时GPS
|
||||
LogUtils.d(TAG, "高速运动:已启动GPS实时更新");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静止/低运动GPS处理:30秒定时更新
|
||||
*/
|
||||
private void handleStaticGPS(MainService mainService) {
|
||||
// 停止实时GPS(已停止则不重复操作)
|
||||
if (mainService.isGpsListening()) {
|
||||
mainService.stopGpsLocation(); // 停止实时更新
|
||||
LogUtils.d(TAG, "静止/低运动:已停止GPS实时更新");
|
||||
}
|
||||
|
||||
// 启动30秒定时GPS(已启动则不重复操作)
|
||||
mainService.startGpsStaticTimer(GPS_STATIC_INTERVAL); // 30秒一次
|
||||
LogUtils.d(TAG, "静止/低运动:已启动GPS30秒定时更新");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取MainService实例(复用逻辑)
|
||||
*/
|
||||
private MainService getMainService() {
|
||||
if (mBroadcastContext == null) return null;
|
||||
|
||||
// 优先获取单例
|
||||
MainService singleton = MainService.getInstance(mBroadcastContext);
|
||||
if (singleton != null && singleton.isServiceRunning()) {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
// 启动服务并重试
|
||||
if (!ServiceUtil.isServiceAlive(mBroadcastContext, MainService.class.getName())) {
|
||||
mBroadcastContext.startService(new Intent(mBroadcastContext, MainService.class));
|
||||
try {
|
||||
Thread.sleep(500); // 等待服务启动
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
return MainService.getInstance(mBroadcastContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送GPS权限申请广播(Receiver无法直接申请)
|
||||
*/
|
||||
private void sendPermissionRequestBroadcast() {
|
||||
Intent permissionIntent = new Intent("cc.winboll.studio.positions.ACTION_REQUEST_GPS_PERMISSION");
|
||||
permissionIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||
mBroadcastContext.sendBroadcast(permissionIntent);
|
||||
LogUtils.d(TAG, "GPS权限缺失,已发送申请广播");
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源清理(核心:传感器不关闭,仅清理Handler和上下文)
|
||||
* @param isForceStopSensor 是否强制停止传感器(仅服务销毁时传true)
|
||||
*/
|
||||
private void cleanResources(boolean isForceStopSensor) {
|
||||
// 1. 停止定时计算任务
|
||||
if (mMainHandler != null) {
|
||||
mMainHandler.removeCallbacksAndMessages(null);
|
||||
mMainHandler = null;
|
||||
LogUtils.d(TAG, "cleanResources():已停止运动状态计算定时器");
|
||||
}
|
||||
|
||||
// 2. 强制停止传感器(仅当外部触发销毁时执行,正常情况不关闭)
|
||||
if (isForceStopSensor && mSensorManager != null && mIsSensorListening) {
|
||||
mSensorManager.unregisterListener(this);
|
||||
mIsSensorListening = false;
|
||||
LogUtils.d(TAG, "cleanResources():已强制停止传感器监听");
|
||||
}
|
||||
|
||||
// 3. 置空上下文(避免内存泄漏)
|
||||
mBroadcastContext = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 传感器精度变化回调(日志监控)
|
||||
*/
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
String sensorType = sensor.getType() == Sensor.TYPE_ACCELEROMETER ? "加速度" : "陀螺仪";
|
||||
String accuracyDesc = getAccuracyDesc(accuracy);
|
||||
LogUtils.d(TAG, sensorType + "传感器精度变化:" + accuracyDesc);
|
||||
}
|
||||
|
||||
/**
|
||||
* 传感器精度描述转换
|
||||
*/
|
||||
private String getAccuracyDesc(int accuracy) {
|
||||
switch (accuracy) {
|
||||
case SensorManager.SENSOR_STATUS_ACCURACY_HIGH: return "高";
|
||||
case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM: return "中";
|
||||
case SensorManager.SENSOR_STATUS_ACCURACY_LOW: return "低";
|
||||
case SensorManager.SENSOR_STATUS_UNRELIABLE: return "不可靠";
|
||||
default: return "未知";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 补充:Receiver销毁时强制清理(需在MainService注销时调用)
|
||||
*/
|
||||
public void forceCleanResources() {
|
||||
cleanResources(true); // 强制停止传感器
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,194 +0,0 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/10 09:51
|
||||
* @Describe 应用图标切换工具类(启用组件时创建对应快捷方式)
|
||||
*/
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.widget.Toast;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.positions.App;
|
||||
import cc.winboll.studio.positions.MainActivity;
|
||||
|
||||
public class APPPlusUtils {
|
||||
public static final String TAG = "APPPlusUtils";
|
||||
|
||||
// 快捷方式配置(名称+图标,需与实际资源匹配)
|
||||
// private static final String PLUS_SHORTCUT_NAME = "位置服务-Laojun";
|
||||
// private static final int PLUS_SHORTCUT_ICON = R.mipmap.ic_launcher; // Laojun 图标资源
|
||||
|
||||
/**
|
||||
* 添加Plus组件与图标
|
||||
*/
|
||||
public static boolean openAPPPlus(Context context) {
|
||||
if (context == null) {
|
||||
LogUtils.d(TAG, "切换失败:上下文为空");
|
||||
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ComponentName plusComponentLaojun = new ComponentName(context, App.COMPONENT_LAOJUN);
|
||||
//ComponentName plusComponentWuKong = new ComponentName(context, MainActivity.COMPONENT_WUKONG);
|
||||
|
||||
try {
|
||||
//disableComponent(pm, plusComponentWuKong);
|
||||
enableComponent(pm, plusComponentLaojun);
|
||||
|
||||
// 2. 创建 Laojun 组件对应的快捷方式(自动去重)
|
||||
// boolean shortcutCreated = createComponentShortcut(context, plusComponent, PLUS_SHORTCUT_NAME, PLUS_SHORTCUT_ICON);
|
||||
//
|
||||
// // 3. 通知桌面刷新图标
|
||||
// context.sendBroadcast(new Intent(Intent.ACTION_PACKAGE_CHANGED)
|
||||
// .setData(android.net.Uri.parse("package:" + context.getPackageName())));
|
||||
//
|
||||
// // 4. 反馈结果
|
||||
// String logMsg = shortcutCreated ? "启用 Laojun + 快捷方式创建成功" : "启用 Laojun 成功,快捷方式创建失败";
|
||||
// String toastMsg = shortcutCreated ? "图标切换为 Laojun,已创建快捷方式" : "图标切换为 Laojun,快捷方式创建失败";
|
||||
// LogUtils.d(TAG, logMsg);
|
||||
// Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
|
||||
//
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "Laojun 图标切换失败:" + e.getMessage());
|
||||
// 失败兜底:启用 Wukong 组件
|
||||
//enableComponent(pm, wukongComponent);
|
||||
Toast.makeText(context, "图标切换失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 移除Plus组件
|
||||
*/
|
||||
public static boolean closeAPPPlus(Context context) {
|
||||
if (context == null) {
|
||||
LogUtils.d(TAG, "切换失败:上下文为空");
|
||||
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ComponentName plusComponentLaojun = new ComponentName(context, App.COMPONENT_LAOJUN);
|
||||
//ComponentName plusComponentWuKong = new ComponentName(context, MainActivity.COMPONENT_WUKONG);
|
||||
|
||||
disableComponent(pm, plusComponentLaojun);
|
||||
//enableComponent(pm, plusComponentWuKong);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建指定组件的桌面快捷方式(自动去重,兼容 Android 8.0+)
|
||||
* @param component 目标组件(如 LAOJUN_ACTIVITY)
|
||||
* @param name 快捷方式名称
|
||||
* @param iconRes 快捷方式图标资源ID
|
||||
* @return 是否创建成功
|
||||
*/
|
||||
private static boolean createComponentShortcut(Context context, ComponentName component, String name, int iconRes) {
|
||||
if (context == null || component == null || name == null || iconRes == 0) {
|
||||
LogUtils.d(TAG, "快捷方式创建失败:参数为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Android 8.0+(API 26+):使用 ShortcutManager(系统推荐)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
try {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
android.content.pm.ShortcutManager shortcutManager = context.getSystemService(android.content.pm.ShortcutManager.class);
|
||||
if (shortcutManager == null || !shortcutManager.isRequestPinShortcutSupported()) {
|
||||
LogUtils.d(TAG, "系统不支持创建快捷方式");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否已存在该组件的快捷方式(去重)
|
||||
for (android.content.pm.ShortcutInfo info : shortcutManager.getPinnedShortcuts()) {
|
||||
if (component.getClassName().equals(info.getIntent().getComponent().getClassName())) {
|
||||
LogUtils.d(TAG, "快捷方式已存在:" + component.getClassName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 构建启动目标组件的意图
|
||||
Intent launchIntent = new Intent(Intent.ACTION_MAIN)
|
||||
.setComponent(component)
|
||||
.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
|
||||
// 构建快捷方式信息
|
||||
android.content.pm.ShortcutInfo shortcutInfo = new android.content.pm.ShortcutInfo.Builder(context, component.getClassName())
|
||||
.setShortLabel(name)
|
||||
.setLongLabel(name)
|
||||
.setIcon(android.graphics.drawable.Icon.createWithResource(context, iconRes))
|
||||
.setIntent(launchIntent)
|
||||
.build();
|
||||
|
||||
// 请求创建快捷方式(需用户确认)
|
||||
shortcutManager.requestPinShortcut(shortcutInfo, null);
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
LogUtils.d(TAG, "Android O+ 快捷方式创建失败:" + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Android 8.0 以下:使用广播(兼容旧机型)
|
||||
try {
|
||||
// 构建启动目标组件的意图
|
||||
Intent launchIntent = new Intent(Intent.ACTION_MAIN)
|
||||
.setComponent(component)
|
||||
.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
|
||||
// 构建创建快捷方式的广播意图
|
||||
Intent installIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
|
||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
|
||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
|
||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
||||
Intent.ShortcutIconResource.fromContext(context, iconRes));
|
||||
installIntent.putExtra("duplicate", false); // 禁止重复创建
|
||||
|
||||
context.sendBroadcast(installIntent);
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
LogUtils.d(TAG, "Android O- 快捷方式创建失败:" + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用组件(带状态检查,避免重复操作)
|
||||
*/
|
||||
private static void enableComponent(PackageManager pm, ComponentName component) {
|
||||
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
|
||||
pm.setComponentEnabledSetting(
|
||||
component,
|
||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
||||
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用组件(带状态检查,避免重复操作)
|
||||
*/
|
||||
private static void disableComponent(PackageManager pm, ComponentName component) {
|
||||
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
|
||||
pm.setComponentEnabledSetting(
|
||||
component,
|
||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.text.TextUtils;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/15 15:23
|
||||
* @Describe Activity Alias 工具类(兼容 Android 所有版本,Java 7 语法)
|
||||
* 用于获取 activity-alias 对应的原始 Activity 组件名、判断 alias 类型、获取启动组件名等
|
||||
*/
|
||||
public class ActivityAliasUtils {
|
||||
private static final String TAG = "ActivityAliasUtils";
|
||||
|
||||
/**
|
||||
* 获取 activity-alias 指向的原始 Activity 组件名
|
||||
*
|
||||
* @param context 上下文(建议用 ApplicationContext)
|
||||
* @param aliasName activity-alias 的组件名(完整路径,如 ".AliasActivity" 或 "com.winboll.app.AliasActivity")
|
||||
* @return 原始 Activity 的完整组件名(如 "com.winboll.app.OriginalActivity"),失败返回 null
|
||||
*/
|
||||
public static String getTargetActivityName(Context context, String aliasName) {
|
||||
// 校验参数
|
||||
if (context == null || TextUtils.isEmpty(aliasName)) {
|
||||
LogUtils.e(TAG, "getTargetActivityName: context is null or aliasName is empty");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 补全组件名(若传入的是短名,自动拼接包名)
|
||||
String fullAliasName = aliasName.startsWith(".")
|
||||
? context.getPackageName() + aliasName
|
||||
: aliasName;
|
||||
|
||||
try {
|
||||
// 1. 获取 PackageManager
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
|
||||
// 2. 解析 activity-alias 的 ActivityInfo(flag 必须设为 PackageManager.GET_META_DATA,否则可能获取不到 targetActivity)
|
||||
ActivityInfo aliasActivityInfo = packageManager.getActivityInfo(
|
||||
new android.content.ComponentName(context.getPackageName(), fullAliasName),
|
||||
PackageManager.GET_META_DATA
|
||||
);
|
||||
|
||||
// 3. 获取 targetActivity(原始 Activity 组件名)
|
||||
String targetActivity = aliasActivityInfo.targetActivity;
|
||||
if (TextUtils.isEmpty(targetActivity)) {
|
||||
LogUtils.e(TAG, "getTargetActivityName: targetActivity is empty for alias " + fullAliasName);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 4. 补全原始 Activity 的完整包名(若 targetActivity 是短名)
|
||||
String fullTargetName = targetActivity.startsWith(".")
|
||||
? context.getPackageName() + targetActivity
|
||||
: targetActivity;
|
||||
|
||||
LogUtils.d(TAG, "getTargetActivityName: alias=" + fullAliasName + ", target=" + fullTargetName);
|
||||
return fullTargetName;
|
||||
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
LogUtils.e(TAG, "getTargetActivityName: alias not found - " + fullAliasName, e);
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "getTargetActivityName: unknown error", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断某个组件名是否为 activity-alias(而非原始 Activity)
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param componentName 待判断的组件名(完整路径)
|
||||
* @return true:是 activity-alias;false:不是或判断失败
|
||||
*/
|
||||
public static boolean isActivityAlias(Context context, String componentName) {
|
||||
// 调用 getTargetActivityName,若返回非空,则说明是 alias
|
||||
return !TextUtils.isEmpty(getTargetActivityName(context, componentName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从启动的 Intent 中获取实际的目标组件名(处理 alias 场景)
|
||||
* 适用于 Activity 中获取自身真实组件名(原始 Activity 名)
|
||||
*
|
||||
* @param context 当前 Activity 上下文
|
||||
* @return 真实的目标组件名(原始 Activity 名,若为 alias 启动则返回原始 Activity,否则返回自身)
|
||||
*/
|
||||
public static String getRealTargetNameFromIntent(Context context) {
|
||||
if (context == null) {
|
||||
LogUtils.e(TAG, "getRealTargetNameFromIntent: context is null");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取当前 Activity 的组件名(可能是 alias)
|
||||
String currentComponentName = context.getClass().getName();
|
||||
// 检查是否为 alias,若是则返回 target,否则返回自身
|
||||
String targetName = getTargetActivityName(context, currentComponentName);
|
||||
return TextUtils.isEmpty(targetName) ? currentComponentName : targetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前活动上下文(Activity)的启动组件名(即启动时使用的组件名,可能是 alias 或原始 Activity)
|
||||
* 场景:若通过 alias 启动 Activity,返回 alias 名;若直接启动原始 Activity,返回原始 Activity 名
|
||||
*
|
||||
* @param context 当前 Activity 上下文(必须是 Activity 实例,不能是 ApplicationContext)
|
||||
* @return 启动组件的完整名,失败返回 null
|
||||
*/
|
||||
public static String getLaunchComponentName(Context context) {
|
||||
// 1. 校验上下文类型(必须是 Activity,否则无法获取启动 Intent)
|
||||
if (context == null) {
|
||||
LogUtils.e(TAG, "getLaunchComponentName: context is null");
|
||||
return null;
|
||||
}
|
||||
if (!(context instanceof android.app.Activity)) {
|
||||
LogUtils.e(TAG, "getLaunchComponentName: context must be Activity instance, current is " + context.getClass().getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 获取启动当前 Activity 的 Intent
|
||||
android.app.Activity activity = (android.app.Activity) context;
|
||||
Intent launchIntent = activity.getIntent();
|
||||
if (launchIntent == null) {
|
||||
LogUtils.e(TAG, "getLaunchComponentName: launch Intent is null");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. 从 Intent 中获取启动组件名(ComponentName)
|
||||
android.content.ComponentName componentName = launchIntent.getComponent();
|
||||
if (componentName == null) {
|
||||
LogUtils.e(TAG, "getLaunchComponentName: ComponentName is null in launch Intent");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 4. 获取组件的完整类名(即启动时使用的组件名)
|
||||
String launchComponentName = componentName.getClassName();
|
||||
LogUtils.d(TAG, "getLaunchComponentName: current launch component is " + launchComponentName);
|
||||
return launchComponentName;
|
||||
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "getLaunchComponentName: failed to get launch component name", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
import android.content.Context;
|
||||
import cc.winboll.studio.positions.AppLevel;
|
||||
import cc.winboll.studio.positions.models.AppConfigsModel;
|
||||
import cc.winboll.studio.positions.App;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
@@ -60,27 +58,11 @@ public class AppConfigsUtil {
|
||||
}
|
||||
|
||||
public void setIsEnableMainService(boolean isEnableMainService) {
|
||||
if (mAppConfigsModel == null) {
|
||||
if(mAppConfigsModel == null) {
|
||||
mAppConfigsModel = new AppConfigsModel();
|
||||
}
|
||||
mAppConfigsModel.setIsEnableMainService(isEnableMainService);
|
||||
saveConfigs();
|
||||
}
|
||||
|
||||
public AppLevel getAppLevel(boolean isReloadConfigs) {
|
||||
if (isReloadConfigs) {
|
||||
loadConfigs();
|
||||
}
|
||||
return (mAppConfigsModel == null) ?AppLevel.WUKONG: mAppConfigsModel.getAppLevel();
|
||||
}
|
||||
|
||||
public void setAppLevel(AppLevel appLevel) {
|
||||
if (mAppConfigsModel == null) {
|
||||
mAppConfigsModel = new AppConfigsModel();
|
||||
}
|
||||
App._mAppLevel = appLevel;
|
||||
mAppConfigsModel.setAppLevel(appLevel);
|
||||
saveConfigs();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,241 +0,0 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/13 15:42
|
||||
* @Describe JsonShareHandler
|
||||
* 外部 JSON 文件分享处理工具类
|
||||
* 功能:接收外部分享的 .json 文件,弹出确认对话框,保存到外部存储 files/BaseBean 目录
|
||||
*/
|
||||
public class JsonShareHandler {
|
||||
private static final String TAG = "JsonShareHandler";
|
||||
private static final String TARGET_DIR = "BaseBean";
|
||||
private static final String MIME_TYPE_JSON = "application/json";
|
||||
private static final String FILE_SUFFIX_JSON = ".json";
|
||||
// 对话框回调接口(Java7 无 Lambda,用接口实现)
|
||||
public interface ConfirmCallback {
|
||||
void onConfirm(boolean isConfirm);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理外部分享的 Intent,先弹出确认对话框,再决定是否接收文件
|
||||
* @param context 上下文(需为 Activity,否则无法弹出对话框)
|
||||
* @param intent 分享 Intent
|
||||
* @param callback 确认结果回调(用于 Activity 处理后续逻辑)
|
||||
*/
|
||||
public static void handleSharedJsonWithConfirm(final Context context, final Intent intent, final ConfirmCallback callback) {
|
||||
if (context == null || intent == null || callback == null) {
|
||||
Log.e(TAG, "参数为空,处理失败");
|
||||
if (callback != null) callback.onConfirm(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 先验证 Intent 合法性(提前过滤无效分享)
|
||||
String action = intent.getAction();
|
||||
String type = intent.getType();
|
||||
if (!Intent.ACTION_SEND.equals(action) || type == null) {
|
||||
Log.e(TAG, "非文件分享 Intent");
|
||||
Toast.makeText(context, "不支持的分享类型", Toast.LENGTH_SHORT).show();
|
||||
callback.onConfirm(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 弹出确认对话框
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle("接收 JSON 文件")
|
||||
.setMessage("是否接收并保存该 JSON 文件?")
|
||||
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
// 3. 点击 Yes,处理文件保存
|
||||
String savedPath = handleSharedJsonFile(context, intent);
|
||||
if (savedPath != null) {
|
||||
Toast.makeText(context, "文件保存成功:" + savedPath, Toast.LENGTH_LONG).show();
|
||||
callback.onConfirm(true);
|
||||
} else {
|
||||
Toast.makeText(context, "文件保存失败", Toast.LENGTH_SHORT).show();
|
||||
callback.onConfirm(false);
|
||||
}
|
||||
}
|
||||
})
|
||||
.setNegativeButton("No", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
// 4. 点击 No,直接退出处理
|
||||
callback.onConfirm(false);
|
||||
}
|
||||
})
|
||||
.setCancelable(false) // 不可点击外部取消
|
||||
.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* 核心文件处理逻辑(原有功能,无修改)
|
||||
*/
|
||||
private static String handleSharedJsonFile(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
String type = intent.getType();
|
||||
|
||||
// 验证 JSON 格式
|
||||
if (!MIME_TYPE_JSON.equals(type) && !type.contains("json")) {
|
||||
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
||||
if (uri == null || !getFileNameFromUri(context, uri).endsWith(FILE_SUFFIX_JSON)) {
|
||||
Log.e(TAG, "接收的文件不是 JSON 格式");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Uri sharedUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
||||
if (sharedUri == null) {
|
||||
Log.e(TAG, "未获取到分享的文件 Uri");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建保存目录
|
||||
File saveDir = getTargetSaveDir(context);
|
||||
if (!saveDir.exists() && !saveDir.mkdirs()) {
|
||||
Log.e(TAG, "创建保存目录失败:" + saveDir.getAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
|
||||
// 解析文件名(兼容低版本)
|
||||
String fileName = getFileNameFromUri(context, sharedUri);
|
||||
if (fileName == null || !fileName.endsWith(FILE_SUFFIX_JSON)) {
|
||||
fileName = "default_" + System.currentTimeMillis() + FILE_SUFFIX_JSON;
|
||||
Log.w(TAG, "文件名解析失败,使用默认名称:" + fileName);
|
||||
}
|
||||
|
||||
// 复制文件
|
||||
File targetFile = new File(saveDir, fileName);
|
||||
boolean copySuccess = copyFileFromUri(context, sharedUri, targetFile);
|
||||
return copySuccess ? targetFile.getAbsolutePath() : null;
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "处理分享文件异常:" + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取目标保存目录(兼容 Android 10+ 分区存储)
|
||||
*/
|
||||
private static File getTargetSaveDir(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
return new File(context.getExternalFilesDir(null), TARGET_DIR);
|
||||
} else {
|
||||
return new File(
|
||||
Environment.getExternalStorageDirectory() + File.separator +
|
||||
"Android" + File.separator +
|
||||
"data" + File.separator +
|
||||
context.getPackageName() + File.separator +
|
||||
"files" + File.separator +
|
||||
TARGET_DIR
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Uri 解析文件名(兼容所有 Android 版本)
|
||||
*/
|
||||
private static String getFileNameFromUri(Context context, Uri uri) {
|
||||
if (uri == null) return null;
|
||||
|
||||
// 1. 文件 Uri(file:// 开头)
|
||||
if ("file".equals(uri.getScheme())) {
|
||||
return new File(uri.getPath()).getName();
|
||||
}
|
||||
|
||||
// 2. 内容 Uri(content:// 开头)
|
||||
if ("content".equals(uri.getScheme())) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = context.getContentResolver().query(
|
||||
uri,
|
||||
new String[]{MediaStore.MediaColumns.DISPLAY_NAME},
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
|
||||
if (nameIndex != -1) {
|
||||
return cursor.getString(nameIndex);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "解析内容 Uri 文件名失败:" + e.getMessage());
|
||||
} finally {
|
||||
if (cursor != null) cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 解析失败,返回默认名称
|
||||
String lastPathSegment = uri.getLastPathSegment();
|
||||
return lastPathSegment != null ? lastPathSegment : "unknown.json";
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制 Uri 指向的文件到目标路径
|
||||
*/
|
||||
private static boolean copyFileFromUri(Context context, Uri sourceUri, File targetFile) {
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
inputStream = context.getContentResolver().openInputStream(sourceUri);
|
||||
if (inputStream == null) {
|
||||
Log.e(TAG, "无法打开源文件输入流");
|
||||
return false;
|
||||
}
|
||||
|
||||
outputStream = new FileOutputStream(targetFile);
|
||||
byte[] buffer = new byte[1024 * 4];
|
||||
int length;
|
||||
while ((length = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, length);
|
||||
}
|
||||
outputStream.flush();
|
||||
Log.d(TAG, "文件保存成功:" + targetFile.getAbsolutePath());
|
||||
return true;
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "文件复制异常:" + e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (inputStream != null) inputStream.close();
|
||||
if (outputStream != null) outputStream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "关闭流异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查外部存储是否可用
|
||||
*/
|
||||
public static boolean isExternalStorageAvailable() {
|
||||
String state = Environment.getExternalStorageState();
|
||||
return Environment.MEDIA_MOUNTED.equals(state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,168 +0,0 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/05 15:49
|
||||
* @Describe LocalMotionDetector
|
||||
*/
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
|
||||
/**
|
||||
* 本机运动状态监测工具(无联网,纯传感器)
|
||||
*/
|
||||
public class LocalMotionDetector implements SensorEventListener {
|
||||
public static final String TAG = "LocalMotionDetector";
|
||||
|
||||
// 配置参数(重点修改:调高运动阈值,适配坐立持机场景)
|
||||
private static final float MOTION_THRESHOLD = 1.8f; // 从0.5f调高到1.8f(过滤坐立轻微晃动)
|
||||
private static final long STATUS_CHECK_INTERVAL = 3000; // 3秒判断一次状态
|
||||
private static final int STEP_CHANGE_THRESHOLD = 2; // 3秒≥2步判定行走
|
||||
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mAccelerometer;
|
||||
private Sensor mStepCounter;
|
||||
private Handler mMainHandler;
|
||||
private MotionStatusCallback mCallback;
|
||||
|
||||
private boolean mIsDetecting = false;
|
||||
private float mLastAccelMagnitude = 0f;
|
||||
private int mLastStepCount = 0;
|
||||
private int mCurrentStepCount = 0;
|
||||
private boolean mIsWalking = false;
|
||||
|
||||
// 单例模式
|
||||
private static LocalMotionDetector sInstance;
|
||||
public static LocalMotionDetector getInstance() {
|
||||
if (sInstance == null) {
|
||||
synchronized (LocalMotionDetector.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new LocalMotionDetector();
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private LocalMotionDetector() {
|
||||
mMainHandler = new Handler(Looper.getMainLooper());
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始监测运动状态
|
||||
*/
|
||||
public void startDetection(Context context, MotionStatusCallback callback) {
|
||||
if (mIsDetecting) return;
|
||||
mCallback = callback;
|
||||
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||
|
||||
// 初始化传感器
|
||||
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
mStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
|
||||
|
||||
// 注册传感器监听
|
||||
if (mAccelerometer != null) {
|
||||
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
|
||||
}
|
||||
if (mStepCounter != null) {
|
||||
mSensorManager.registerListener(this, mStepCounter, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
|
||||
LogUtils.d(TAG, "计步传感器已启动");
|
||||
} else {
|
||||
LogUtils.d(TAG, "设备不支持计步传感器,仅用加速度判断");
|
||||
}
|
||||
|
||||
// 启动定时状态检测
|
||||
mMainHandler.postDelayed(mStatusCheckRunnable, STATUS_CHECK_INTERVAL);
|
||||
mIsDetecting = true;
|
||||
LogUtils.d(TAG, "运动状态监测已启动");
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止监测
|
||||
*/
|
||||
public void stopDetection() {
|
||||
if (!mIsDetecting) return;
|
||||
if (mSensorManager != null) {
|
||||
mSensorManager.unregisterListener(this);
|
||||
}
|
||||
mMainHandler.removeCallbacksAndMessages(null);
|
||||
mIsDetecting = false;
|
||||
mIsWalking = false;
|
||||
mCallback = null;
|
||||
LogUtils.d(TAG, "运动状态监测已停止");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if (!mIsDetecting) return;
|
||||
switch (event.sensor.getType()) {
|
||||
case Sensor.TYPE_ACCELEROMETER:
|
||||
// 计算加速度幅度(保留原逻辑,阈值已调高)
|
||||
float accelX = Math.abs(event.values[0]);
|
||||
float accelY = Math.abs(event.values[1]);
|
||||
float accelZ = Math.abs(event.values[2]);
|
||||
mLastAccelMagnitude = accelX + accelY + accelZ;
|
||||
break;
|
||||
case Sensor.TYPE_STEP_COUNTER:
|
||||
// 累计步数
|
||||
mCurrentStepCount = (int) event.values[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时判断运动状态(优化逻辑:计步为0时,即使有轻微加速度也判定为静止)
|
||||
*/
|
||||
private final Runnable mStatusCheckRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!mIsDetecting || mCallback == null) return;
|
||||
|
||||
//LogUtils.d(TAG, "mStatusCheckRunnable run");
|
||||
|
||||
boolean newIsWalking = false;
|
||||
// 结合计步器+加速度判断(优化:优先计步,无步数时严格按高阈值判断)
|
||||
if (mStepCounter != null) {
|
||||
int stepChange = mCurrentStepCount - mLastStepCount;
|
||||
// 只有“步数达标” 或 “无步数但加速度远超坐立幅度”,才判定为行走
|
||||
newIsWalking = (stepChange >= STEP_CHANGE_THRESHOLD)
|
||||
&& (mLastAccelMagnitude >= MOTION_THRESHOLD); // 增加步数+加速度双重校验
|
||||
mLastStepCount = mCurrentStepCount;
|
||||
} else {
|
||||
// 无计步器时,仅用高阈值判断
|
||||
newIsWalking = mLastAccelMagnitude >= MOTION_THRESHOLD;
|
||||
}
|
||||
|
||||
// 状态变化时回调
|
||||
if (newIsWalking != mIsWalking) {
|
||||
mIsWalking = newIsWalking;
|
||||
String statusDesc = mIsWalking ? "行走状态" : "静止/低运动状态";
|
||||
LogUtils.d(TAG, "运动状态变化:" + statusDesc + " | 加速度幅度:" + mLastAccelMagnitude); // 增加日志便于调试
|
||||
mCallback.onMotionStatusChanged(mIsWalking, statusDesc);
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, String.format("运动状态 newIsWalking %s", newIsWalking));
|
||||
|
||||
// 循环检测
|
||||
mMainHandler.postDelayed(this, STATUS_CHECK_INTERVAL);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
||||
|
||||
/**
|
||||
* 运动状态回调接口
|
||||
*/
|
||||
public interface MotionStatusCallback {
|
||||
void onMotionStatusChanged(boolean isWalking, String statusDesc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.positions.PointLevel;
|
||||
import cc.winboll.studio.positions.activities.WinBoLLActivity;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/15 15:59
|
||||
* @Describe 应用活动窗口状态响应类
|
||||
* 主要用于设置应用级别与组件状态
|
||||
*/
|
||||
public class MyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
|
||||
|
||||
public static final String TAG = "MyActivityLifecycleCallbacks";
|
||||
|
||||
public String mInfo = "";
|
||||
|
||||
public MyActivityLifecycleCallbacks() {
|
||||
|
||||
}
|
||||
|
||||
void createActivityeInfo(Activity activity) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Intent receivedIntent = activity.getIntent();
|
||||
sb.append("\nCallingActivity : \n");
|
||||
if (activity.getCallingActivity() != null) {
|
||||
sb.append(activity.getCallingActivity().getPackageName());
|
||||
}
|
||||
sb.append("\nReceived Intent Package : \n");
|
||||
sb.append(receivedIntent.getPackage());
|
||||
|
||||
Bundle extras = receivedIntent.getExtras();
|
||||
if (extras != null) {
|
||||
for (String key : extras.keySet()) {
|
||||
sb.append("\nIntentInfo");
|
||||
sb.append("\n键: ");
|
||||
sb.append(key);
|
||||
sb.append(", 值: ");
|
||||
sb.append(extras.get(key));
|
||||
//Log.d("IntentInfo", "键: " + key + ", 值: " + extras.get(key));
|
||||
}
|
||||
}
|
||||
mInfo = sb.toString();
|
||||
//Log.d("IntentInfo", "发送Intent的应用包名: " + senderPackage);
|
||||
}
|
||||
|
||||
public void showActivityeInfo() {
|
||||
//ToastUtils.show("ActivityeInfo : " + mInfo);
|
||||
LogUtils.d(TAG, "ActivityeInfo : " + mInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
||||
// 在这里可以做一些初始化相关的操作,例如记录Activity的创建时间等
|
||||
//System.out.println(activity.getLocalClassName() + " was created");
|
||||
LogUtils.d(TAG, activity.getLocalClassName() + " was created");
|
||||
createActivityeInfo(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStarted(Activity activity) {
|
||||
//System.out.println(activity.getLocalClassName() + " was started");
|
||||
LogUtils.d(TAG, activity.getLocalClassName() + " was started");
|
||||
//createActivityeInfo(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResumed(Activity activity) {
|
||||
//System.out.println(activity.getLocalClassName() + " was resumed");
|
||||
LogUtils.d(TAG, activity.getLocalClassName() + " was resumed");
|
||||
//createActivityeInfo(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityPaused(Activity activity) {
|
||||
ToastUtils.show("Activity Paused");
|
||||
// 应用从正在活动状态抽离出来时,设置应用入口级别状态,设置为时空虚幻而不确定的哆啦A梦级别。
|
||||
WinBoLLActivity._mPointLevel = PointLevel.DORAEMON;
|
||||
//System.out.println(activity.getLocalClassName() + " was paused");
|
||||
LogUtils.d(TAG, activity.getLocalClassName() + " was paused");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStopped(Activity activity) {
|
||||
//System.out.println(activity.getLocalClassName() + " was stopped");
|
||||
LogUtils.d(TAG, activity.getLocalClassName() + " was stopped");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
|
||||
// 可以在这里添加保存状态的自定义逻辑
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityDestroyed(Activity activity) {
|
||||
//System.out.println(activity.getLocalClassName() + " was destroyed");
|
||||
LogUtils.d(TAG, activity.getLocalClassName() + " was destroyed");
|
||||
}
|
||||
}
|
||||
@@ -1,282 +0,0 @@
|
||||
package cc.winboll.studio.positions.views;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/10 08:29
|
||||
* @Describe 沙漏计时器控件
|
||||
*/
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ClipDrawable;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.text.InputFilter;
|
||||
import android.text.InputType;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* 沙漏视图类(Java 7语法,修复ProgressDrawable和setHeight问题)
|
||||
*/
|
||||
public class HourglassView extends LinearLayout {
|
||||
public static final String TAG = "HourglassView";
|
||||
// 数据模型
|
||||
private String hourglassId;
|
||||
private int hour; // 小时
|
||||
private int minute; // 分钟
|
||||
private boolean isEnabled; // 开关状态
|
||||
|
||||
// 控件引用
|
||||
private EditText etHour;
|
||||
private EditText etMinute;
|
||||
private ProgressBar progressBar;
|
||||
private Switch switchControl;
|
||||
|
||||
// 样式参数
|
||||
private int textSize = 16;
|
||||
private int padding = 8;
|
||||
private int progressColor = 0xFF2196F3; // 进度条颜色
|
||||
private int progressBgColor = 0xFFE0E0E0; // 进度条背景色
|
||||
private int textColor = 0xFF333333;
|
||||
private int editTextWidth = 40; // 输入框宽度(dp)
|
||||
private int progressHeight = 8; // 进度条高度(dp,新增参数)
|
||||
|
||||
public HourglassView(Context context) {
|
||||
super(context);
|
||||
initView();
|
||||
}
|
||||
|
||||
public HourglassView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initView();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化视图布局
|
||||
*/
|
||||
private void initView() {
|
||||
setOrientation(HORIZONTAL);
|
||||
setGravity(Gravity.CENTER_VERTICAL);
|
||||
setPadding(dp2px(padding), dp2px(padding), dp2px(padding), dp2px(padding));
|
||||
|
||||
// 1. 左侧时间输入区域(水平布局)
|
||||
LinearLayout inputLayout = new LinearLayout(getContext());
|
||||
inputLayout.setOrientation(HORIZONTAL);
|
||||
inputLayout.setGravity(Gravity.CENTER_VERTICAL);
|
||||
LayoutParams inputParams = new LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
inputParams.setMargins(0, 0, dp2px(padding * 2), 0);
|
||||
addView(inputLayout, inputParams);
|
||||
|
||||
// 小时输入框
|
||||
etHour = createNumberEditText();
|
||||
etHour.setHint("时");
|
||||
etHour.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
|
||||
inputLayout.addView(etHour, getEditTextParams());
|
||||
|
||||
// 分隔符
|
||||
TextView divider = new TextView(getContext());
|
||||
divider.setText(":");
|
||||
divider.setTextSize(textSize);
|
||||
divider.setTextColor(textColor);
|
||||
LayoutParams dividerParams = new LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
dividerParams.setMargins(dp2px(padding / 2), 0, dp2px(padding / 2), 0);
|
||||
inputLayout.addView(divider, dividerParams);
|
||||
|
||||
// 分钟输入框
|
||||
etMinute = createNumberEditText();
|
||||
etMinute.setHint("分");
|
||||
etMinute.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
|
||||
inputLayout.addView(etMinute, getEditTextParams());
|
||||
|
||||
// 2. 中间进度条(修复:通过LayoutParams设置高度,替代setHeight)
|
||||
progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleHorizontal);
|
||||
progressBar.setProgressDrawable(createProgressDrawable()); // 传入Drawable类型
|
||||
|
||||
// 修复核心:用LayoutParams设置进度条高度(兼容低版本)
|
||||
LayoutParams progressParams = new LayoutParams(
|
||||
0,
|
||||
dp2px(progressHeight), // 直接在布局参数中设置高度(dp转px)
|
||||
1.0f
|
||||
);
|
||||
progressParams.setMargins(0, 0, dp2px(padding * 2), 0);
|
||||
addView(progressBar, progressParams);
|
||||
|
||||
// 3. 右侧开关
|
||||
switchControl = new Switch(getContext());
|
||||
switchControl.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
|
||||
isEnabled = isChecked;
|
||||
// 开关状态控制输入框是否可编辑
|
||||
etHour.setEnabled(!isChecked);
|
||||
etMinute.setEnabled(!isChecked);
|
||||
// 更新进度条(仅在开关开启时生效)
|
||||
if (isChecked) {
|
||||
updateProgressBar();
|
||||
}
|
||||
}
|
||||
});
|
||||
addView(switchControl);
|
||||
|
||||
// 初始状态
|
||||
isEnabled = false;
|
||||
etHour.setEnabled(true);
|
||||
etMinute.setEnabled(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数字输入框
|
||||
*/
|
||||
private EditText createNumberEditText() {
|
||||
EditText editText = new EditText(getContext());
|
||||
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||
editText.setTextSize(textSize);
|
||||
editText.setTextColor(textColor);
|
||||
editText.setGravity(Gravity.CENTER);
|
||||
editText.setSingleLine(true);
|
||||
editText.setBackgroundResource(android.R.drawable.edit_text); // 默认输入框背景
|
||||
return editText;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取输入框布局参数
|
||||
*/
|
||||
private LayoutParams getEditTextParams() {
|
||||
LayoutParams params = new LayoutParams(
|
||||
dp2px(editTextWidth),
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
params.setMargins(0, 0, dp2px(padding), 0);
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复核心:创建ProgressDrawable(返回Drawable类型,而非Paint)
|
||||
* 用LayerDrawable实现「背景+进度」的双层进度条
|
||||
*/
|
||||
private Drawable createProgressDrawable() {
|
||||
// 1. 进度条背景(灰色)
|
||||
ColorDrawable bgDrawable = new ColorDrawable(progressBgColor);
|
||||
// 2. 进度条前景(主题色)
|
||||
ColorDrawable progressDrawable = new ColorDrawable(progressColor);
|
||||
// 3. 用ClipDrawable包裹前景,实现进度裁剪
|
||||
ClipDrawable clipDrawable = new ClipDrawable(progressDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
|
||||
|
||||
// 4. 组合成LayerDrawable(顺序:背景在下,进度在上)
|
||||
Drawable[] layers = new Drawable[]{bgDrawable, clipDrawable};
|
||||
LayerDrawable layerDrawable = new LayerDrawable(layers);
|
||||
|
||||
// 5. 设置进度条的层级ID(必须与系统ProgressBar的ID匹配)
|
||||
layerDrawable.setId(0, android.R.id.background);
|
||||
layerDrawable.setId(1, android.R.id.progress);
|
||||
|
||||
return layerDrawable;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新进度条(总时间 = 小时*60 + 分钟,单位:分钟)
|
||||
*/
|
||||
private void updateProgressBar() {
|
||||
try {
|
||||
// 获取输入的时间(为空时默认0)
|
||||
int inputHour = TextUtils.isEmpty(etHour.getText().toString().trim())
|
||||
? 0 : Integer.parseInt(etHour.getText().toString().trim());
|
||||
int inputMinute = TextUtils.isEmpty(etMinute.getText().toString().trim())
|
||||
? 0 : Integer.parseInt(etMinute.getText().toString().trim());
|
||||
|
||||
// 校验时间合法性(小时0-99,分钟0-59)
|
||||
inputHour = Math.max(0, Math.min(99, inputHour));
|
||||
inputMinute = Math.max(0, Math.min(59, inputMinute));
|
||||
|
||||
// 计算总分钟数(进度条最大值)
|
||||
int totalMinutes = inputHour * 60 + inputMinute;
|
||||
totalMinutes = Math.max(1, totalMinutes); // 最小1分钟,避免进度条无长度
|
||||
|
||||
// 更新进度条
|
||||
progressBar.setMax(totalMinutes);
|
||||
progressBar.setProgress(totalMinutes); // 初始显示满进度,可根据实际需求修改
|
||||
|
||||
// 更新数据模型
|
||||
this.hour = inputHour;
|
||||
this.minute = inputMinute;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
// 输入非法时重置进度条
|
||||
progressBar.setMax(0);
|
||||
progressBar.setProgress(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* dp转px(适配不同设备)
|
||||
*/
|
||||
private int dp2px(int dp) {
|
||||
return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
|
||||
}
|
||||
|
||||
// ------------------- 数据模型 getter/setter -------------------
|
||||
public String getHourglassId() {
|
||||
return hourglassId;
|
||||
}
|
||||
|
||||
public void setHourglassId(String hourglassId) {
|
||||
this.hourglassId = hourglassId;
|
||||
}
|
||||
|
||||
public int getHour() {
|
||||
return hour;
|
||||
}
|
||||
|
||||
public void setHour(int hour) {
|
||||
this.hour = Math.max(0, Math.min(99, hour)); // 限制范围
|
||||
etHour.setText(String.valueOf(this.hour));
|
||||
}
|
||||
|
||||
public int getMinute() {
|
||||
return minute;
|
||||
}
|
||||
|
||||
public void setMinute(int minute) {
|
||||
this.minute = Math.max(0, Math.min(59, minute)); // 限制范围
|
||||
etMinute.setText(String.valueOf(this.minute));
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
isEnabled = enabled;
|
||||
switchControl.setChecked(enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动更新进度条(外部调用)
|
||||
*/
|
||||
public void refreshProgress() {
|
||||
if (isEnabled) {
|
||||
updateProgressBar();
|
||||
}
|
||||
}
|
||||
|
||||
// 工具类:判断字符串是否为空(Java7无TextUtils.isEmpty,手动实现)
|
||||
private static class TextUtils {
|
||||
public static boolean isEmpty(CharSequence str) {
|
||||
return str == null || str.length() == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,10 +33,6 @@ import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import cc.winboll.studio.positions.App;
|
||||
import cc.winboll.studio.positions.AppLevel;
|
||||
import cc.winboll.studio.positions.activities.WinBoLLActivity;
|
||||
import cc.winboll.studio.positions.PointLevel;
|
||||
|
||||
public class PositionTaskListView extends LinearLayout {
|
||||
// 视图模式常量
|
||||
@@ -384,7 +380,7 @@ public class PositionTaskListView extends LinearLayout {
|
||||
// 步骤3:刷新Adapter(局部刷新+范围通知,避免列表错乱)
|
||||
notifyItemRemoved(position);
|
||||
notifyItemRangeChanged(position, mAdapterData.size());
|
||||
|
||||
|
||||
LogUtils.d(TAG, "Adapter已移除任务,刷新列表(位置索引=" + position + ")");
|
||||
|
||||
// 步骤4:通知外部(如Activity)任务已更新
|
||||
@@ -461,7 +457,7 @@ public class PositionTaskListView extends LinearLayout {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private String genSelectedTimeText(long timeMillis) {
|
||||
// 2. 格式化时间字符串(Java 7 用 SimpleDateFormat,需处理 ParseException)
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
|
||||
@@ -487,16 +483,6 @@ public class PositionTaskListView extends LinearLayout {
|
||||
final EditText etEditDistance = dialogView.findViewById(R.id.et_edit_distance);
|
||||
Button btnCancel = dialogView.findViewById(R.id.btn_dialog_cancel);
|
||||
Button btnSave = dialogView.findViewById(R.id.btn_dialog_save);
|
||||
HourglassView hourglassView = dialogView.findViewById(R.id.hourglassView);
|
||||
if (WinBoLLActivity._mPointLevel == PointLevel.WUKONG) {
|
||||
hourglassView.setVisibility(View.GONE);
|
||||
} else if (WinBoLLActivity._mPointLevel == PointLevel.LAOJUN) {
|
||||
hourglassView.setHourglassId("hourglass_001");
|
||||
hourglassView.setHour(1);
|
||||
hourglassView.setMinute(30);
|
||||
hourglassView.setEnabled(false); // 开启开关
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 绑定外层对话框内的控件
|
||||
|
||||
@@ -65,16 +65,6 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<cc.winboll.studio.positions.views.HourglassView
|
||||
android:id="@+id/hourglassView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
@@ -87,7 +77,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:text="开始时间"
|
||||
android:id="@+id/btn_select_time"/>
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_selected_time"
|
||||
android:layout_width="0dp"
|
||||
@@ -95,6 +85,7 @@
|
||||
android:text="Text"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">悟空笔记</string>
|
||||
<string name="appplus_name">时空任务</string>
|
||||
<string name="open_appplus">开疆扩土</string>
|
||||
<string name="close_appplus">返璞归真</string>
|
||||
<string name="appplus_open_disabled">余力不足</string>
|
||||
<string name="appplus_close_disabled">辎重难返</string>
|
||||
</resources>
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
<resources>
|
||||
<string name="app_name">Positions</string>
|
||||
<string name="appplus_name">PositionsPlus</string>
|
||||
<string name="appplus_name">PositionsPlus</string>
|
||||
<string name="open_appplus">Open APP Plus</string>
|
||||
<string name="close_appplus">Close APP Plus</string>
|
||||
<string name="appplus_open_disabled">APP Plus Open Disable</string>
|
||||
<string name="appplus_close_disabled">APP Plus Close Disable</string>
|
||||
</resources>
|
||||
|
||||
|
||||
6
positions/src/main/res/xml/file_provider.xml
Normal file
6
positions/src/main/res/xml/file_provider.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<external-files-path
|
||||
name="BaseBean"
|
||||
path="BaseBean/" />
|
||||
</paths>
|
||||
@@ -33,7 +33,7 @@ android {
|
||||
// versionName 更新后需要手动设置
|
||||
// .winboll/winbollBuildProps.properties 文件的 stageCount=0
|
||||
// Gradle编译环境下合起来的 versionName 就是 "${versionName}.0"
|
||||
versionName "15.12"
|
||||
versionName "15.11"
|
||||
if(true) {
|
||||
versionName = genVersionName("${versionName}")
|
||||
}
|
||||
@@ -56,12 +56,7 @@ dependencies {
|
||||
api 'com.github.bumptech.glide:glide:4.9.0'
|
||||
//annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
|
||||
|
||||
// uCrop 核心依赖(最新稳定版)
|
||||
implementation 'com.github.yalantis:ucrop:2.2.8'
|
||||
// 兼容AndroidX(若项目用AndroidX,必须添加)
|
||||
//implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||
implementation 'androidx.exifinterface:exifinterface:1.3.6'
|
||||
|
||||
|
||||
// 应用介绍页类库
|
||||
api 'io.github.medyo:android-about-page:2.0.0'
|
||||
// SSH
|
||||
@@ -82,13 +77,8 @@ dependencies {
|
||||
//api 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
|
||||
//api 'androidx.fragment:fragment:1.1.0'
|
||||
|
||||
// WinBoLL库 nexus.winboll.cc 地址
|
||||
//api 'cc.winboll.studio:libaes:15.12.0'
|
||||
//api 'cc.winboll.studio:libappbase:15.12.2'
|
||||
|
||||
// WinBoLL备用库 jitpack.io 地址
|
||||
api 'com.github.ZhanGSKen:AES:aes-v15.12.3'
|
||||
api 'com.github.ZhanGSKen:APPBase:appbase-v15.12.2'
|
||||
implementation 'cc.winboll.studio:libaes:15.11.6'
|
||||
implementation 'cc.winboll.studio:libappbase:15.11.0'
|
||||
|
||||
//api fileTree(dir: 'libs', include: ['*.aar'])
|
||||
api fileTree(dir: 'libs', include: ['*.jar'])
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Wed Dec 10 16:54:47 HKT 2025
|
||||
stageCount=5
|
||||
#Wed Nov 26 16:27:33 HKT 2025
|
||||
stageCount=9
|
||||
libraryProject=
|
||||
baseVersion=15.12
|
||||
publishVersion=15.12.4
|
||||
baseVersion=15.11
|
||||
publishVersion=15.11.8
|
||||
buildCount=0
|
||||
baseBetaVersion=15.12.5
|
||||
baseBetaVersion=15.11.9
|
||||
|
||||
@@ -150,7 +150,7 @@
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="cc.winboll.studio.powerbell.activities.BackgroundSettingsActivity"
|
||||
android:name="cc.winboll.studio.powerbell.activities.BackgroundPictureActivity"
|
||||
android:parentActivityName="cc.winboll.studio.powerbell.MainActivity"
|
||||
android:exported="true"
|
||||
android:launchMode="singleTask">
|
||||
@@ -208,6 +208,8 @@
|
||||
|
||||
<activity android:name="cc.winboll.studio.powerbell.activities.BatteryReporterActivity"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.powerbell.activities.AboutActivity"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.powerbell.activities.PixelPickerActivity"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.powerbell.activities.BatteryReportActivity"/>
|
||||
@@ -228,15 +230,6 @@
|
||||
|
||||
<activity android:name="cc.winboll.studio.powerbell.activities.ShortcutActionActivity"/>
|
||||
|
||||
<activity android:name="cc.winboll.studio.powerbell.activities.SettingsActivity"/>
|
||||
|
||||
<!-- 1. 注册 UCropActivity(关键:解决崩溃) -->
|
||||
<activity
|
||||
android:name="com.yalantis.ucrop.UCropActivity"
|
||||
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
|
||||
android:exported="true"> <!-- 必须添加:Android 12+ 要求显式声明 exported -->
|
||||
</activity>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
</manifest>
|
||||
@@ -2,7 +2,7 @@ package cc.winboll.studio.powerbell;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import android.view.Gravity;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.receivers.GlobalApplicationReceiver;
|
||||
@@ -12,15 +12,15 @@ import java.io.File;
|
||||
|
||||
public class App extends GlobalApplication {
|
||||
|
||||
public static final String TAG = "App";
|
||||
|
||||
public static final String TAG = "GlobalApplication";
|
||||
|
||||
public static final String COMPONENT_EN1 = "cc.winboll.studio.powerbell.MainActivityEN1";
|
||||
public static final String COMPONENT_CN1 = "cc.winboll.studio.powerbell.MainActivityCN1";
|
||||
public static final String COMPONENT_CN2 = "cc.winboll.studio.powerbell.MainActivityCN2";
|
||||
public static final String ACTION_SWITCHTO_EN1 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_EN1";
|
||||
public static final String ACTION_SWITCHTO_CN1 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN1";
|
||||
public static final String ACTION_SWITCHTO_CN2 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN2";
|
||||
|
||||
|
||||
// 数据配置存储工具
|
||||
static AppConfigUtils _mAppConfigUtils;
|
||||
static AppCacheUtils _mAppCacheUtils;
|
||||
@@ -35,19 +35,13 @@ public class App extends GlobalApplication {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
setIsDebugging(BuildConfig.DEBUG);
|
||||
//setIsDebugging(false);
|
||||
|
||||
// 初始化活动窗口管理
|
||||
WinBoLLActivityManager.init(this);
|
||||
// 初始化 Toast 框架
|
||||
ToastUtils.init(this);
|
||||
|
||||
// 临时文件夹方案1
|
||||
// 获取Pictures文件夹路径(Android 10及以上推荐使用MediaStore,此处为传统方式)
|
||||
File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||
// 定义目标文件路径(在Pictures目录下创建"PowerBell"子文件夹及文件)
|
||||
File powerBellDir = new File(picturesDir, "PowerBell");
|
||||
|
||||
|
||||
// 临时文件夹方案2 <图片保存失败>
|
||||
// 获取Pictures文件夹路径(Android 10及以上推荐使用MediaStore,此处为传统方式)
|
||||
//File powerBellDir = getExternalFilesDir("TempDir");
|
||||
@@ -58,6 +52,14 @@ public class App extends GlobalApplication {
|
||||
}
|
||||
szTempDir = powerBellDir.getAbsolutePath();
|
||||
|
||||
|
||||
// 初始化 Toast 框架
|
||||
ToastUtils.init(this);
|
||||
// 设置 Toast 布局样式
|
||||
//ToastUtils.setView(R.layout.toast_custom_view);
|
||||
//ToastUtils.setStyle(new WhiteToastStyle());
|
||||
//ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
|
||||
|
||||
// 设置数据配置存储工具
|
||||
_mAppConfigUtils = getAppConfigUtils(this);
|
||||
_mAppCacheUtils = getAppCacheUtils(this);
|
||||
@@ -89,7 +91,7 @@ public class App extends GlobalApplication {
|
||||
super.onTerminate();
|
||||
ToastUtils.release();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,65 @@
|
||||
package cc.winboll.studio.powerbell.activities;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2025/03/25 01:16:32
|
||||
* @Describe 应用介绍窗口
|
||||
*/
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
import cc.winboll.studio.libaes.models.APPInfo;
|
||||
import cc.winboll.studio.libaes.views.AToolbar;
|
||||
import cc.winboll.studio.libaes.views.AboutView;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
|
||||
public class AboutActivity extends Activity {
|
||||
|
||||
Context mContext;
|
||||
|
||||
public static final String TAG = "AboutActivity";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_about);
|
||||
mContext = this;
|
||||
|
||||
// 初始化工具栏
|
||||
AToolbar mAToolbar = (AToolbar) findViewById(R.id.toolbar);
|
||||
setActionBar(mAToolbar);
|
||||
mAToolbar.setSubtitle(getString(R.string.text_about));
|
||||
//mAToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
AboutView aboutView = CreateAboutView();
|
||||
// 在 Activity 的 onCreate 或其他生命周期方法中调用
|
||||
LinearLayout llRoot = findViewById(R.id.root_ll);
|
||||
//layout.setOrientation(LinearLayout.VERTICAL);
|
||||
// 创建布局参数(宽度和高度)
|
||||
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
);
|
||||
llRoot.addView(aboutView, params);
|
||||
|
||||
}
|
||||
|
||||
public AboutView CreateAboutView() {
|
||||
String szBranchName = "powerbell";
|
||||
APPInfo appInfo = new APPInfo();
|
||||
appInfo.setAppName(getString(R.string.app_name));
|
||||
appInfo.setAppIcon(R.drawable.ic_launcher);
|
||||
appInfo.setAppDescription(getString(R.string.app_description));
|
||||
appInfo.setAppGitName("APPBase");
|
||||
appInfo.setAppGitOwner("Studio");
|
||||
appInfo.setAppGitAPPBranch(szBranchName);
|
||||
appInfo.setAppGitAPPSubProjectFolder(szBranchName);
|
||||
appInfo.setAppHomePage("https://www.winboll.cc/apks/index.php?project=PowerBell");
|
||||
appInfo.setAppAPKName("PowerBell");
|
||||
appInfo.setAppAPKFolderName("PowerBell");
|
||||
return new AboutView(mContext, appInfo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,659 @@
|
||||
package cc.winboll.studio.powerbell.activities;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.provider.MediaStore;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RelativeLayout;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import cc.winboll.studio.libaes.dialogs.YesNoAlertDialog;
|
||||
import cc.winboll.studio.libaes.views.AToolbar;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.App;
|
||||
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.FileUtils;
|
||||
import cc.winboll.studio.powerbell.utils.UriUtil;
|
||||
import cc.winboll.studio.powerbell.views.BackgroundView;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class BackgroundPictureActivity extends WinBoLLActivity implements BackgroundPicturePreviewDialog.IOnRecivedPictureListener {
|
||||
|
||||
public static final String TAG = "BackgroundPictureActivity";
|
||||
public BackgroundPictureUtils mBackgroundPictureUtils;
|
||||
|
||||
// 图片选择请求码
|
||||
public static final int REQUEST_SELECT_PICTURE = 0;
|
||||
public static final int REQUEST_TAKE_PHOTO = 1;
|
||||
public static final int REQUEST_CROP_IMAGE = 2;
|
||||
private static final int STORAGE_PERMISSION_REQUEST = 100;
|
||||
|
||||
private AToolbar mAToolbar;
|
||||
private File mfBackgroundDir; // 背景图片存储文件夹
|
||||
private File mfPictureDir; // 拍照与剪裁临时文件夹
|
||||
private File mfTakePhoto; // 拍照文件
|
||||
private File mfRecivedPicture; // 接收的图片文件
|
||||
private File mfTempCropPicture; // 剪裁临时文件
|
||||
private File mfRecivedCropPicture; // 剪裁后的目标文件
|
||||
|
||||
private String preViewFileBackgroundView = "";
|
||||
BackgroundView bvPreviewBackground;
|
||||
boolean isCommitSettings = false;
|
||||
|
||||
// 静态变量
|
||||
public static String _mszRecivedCropPicture = "RecivedCrop.jpg";
|
||||
private static String _mszCommonFileType = "jpeg";
|
||||
private int mnPictureCompress = 100;
|
||||
private static String _RecivedPictureFileName;
|
||||
|
||||
@Override
|
||||
public Activity getActivity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_backgroundpicture);
|
||||
initEnv();
|
||||
|
||||
// 初始化工具类和文件夹
|
||||
mBackgroundPictureUtils = BackgroundPictureUtils.getInstance(this);
|
||||
mfBackgroundDir = new File(mBackgroundPictureUtils.getBackgroundDir());
|
||||
if (!mfBackgroundDir.exists()) {
|
||||
mfBackgroundDir.mkdirs();
|
||||
}
|
||||
|
||||
mfPictureDir = new File(App.getTempDirPath());
|
||||
if (!mfPictureDir.exists()) {
|
||||
mfPictureDir.mkdirs();
|
||||
}
|
||||
|
||||
// 初始化文件对象
|
||||
mfTakePhoto = new File(mfPictureDir, "TakePhoto.jpg");
|
||||
mfTempCropPicture = new File(mfPictureDir, "TempCrop.jpg");
|
||||
|
||||
mfRecivedPicture = getRecivedPictureFile(this);
|
||||
mfRecivedCropPicture = new File(mfBackgroundDir, _mszRecivedCropPicture);
|
||||
|
||||
// 初始化工具栏
|
||||
mAToolbar = (AToolbar) findViewById(R.id.toolbar);
|
||||
setActionBar(mAToolbar);
|
||||
mAToolbar.setSubtitle(R.string.subtitle_activity_backgroundpicture);
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mAToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish(); // 点击导航栏返回按钮,触发 finish()
|
||||
}
|
||||
});
|
||||
|
||||
// 设置按钮点击事件
|
||||
findViewById(R.id.activitybackgroundpictureAButton5).setOnClickListener(onOriginNullClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton4).setOnClickListener(onReceivedPictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton1).setOnClickListener(onTakePhotoClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton2).setOnClickListener(onSelectPictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton3).setOnClickListener(onCropPictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton6).setOnClickListener(onCropFreePictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton7).setOnClickListener(onPixelPickerClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton8).setOnClickListener(onCleanPixelClickListener);
|
||||
|
||||
updatePreviewBackground();
|
||||
|
||||
// 处理分享的图片
|
||||
Intent intent = getIntent();
|
||||
String action = intent.getAction();
|
||||
String type = intent.getType();
|
||||
|
||||
if (Intent.ACTION_SEND.equals(action) && type != null && isImageType(type)) {
|
||||
BackgroundPicturePreviewDialog dlg = new BackgroundPicturePreviewDialog(this);
|
||||
dlg.show();
|
||||
}
|
||||
}
|
||||
|
||||
private void initEnv() {
|
||||
LogUtils.d(TAG, "initEnv()");
|
||||
_RecivedPictureFileName = "Recived.data";
|
||||
}
|
||||
|
||||
public static String getBackgroundFileName() {
|
||||
return _mszRecivedCropPicture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAcceptRecivedPicture(String szPreRecivedPictureName) {
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
|
||||
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
|
||||
utils.saveData();
|
||||
|
||||
File sourceFile = new File(utils.getBackgroundDir(), szPreRecivedPictureName);
|
||||
if (FileUtils.copyFile(sourceFile, mfRecivedPicture)) {
|
||||
startCropImageActivity(false);
|
||||
} else {
|
||||
ToastUtils.show("图片复制失败,请重试");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新背景图片预览
|
||||
*/
|
||||
public void updatePreviewBackground() {
|
||||
LogUtils.d(TAG, "updatePreviewBackground");
|
||||
//ImageView ivPreviewBackground = (ImageView) findViewById(R.id.activitybackgroundpictureImageView1);
|
||||
bvPreviewBackground = (BackgroundView) findViewById(R.id.activitybackgroundpictureBackgroundView1);
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
|
||||
utils.loadBackgroundPictureBean();
|
||||
|
||||
boolean isUseBackgroundFile = utils.getBackgroundPictureBean().isUseBackgroundFile();
|
||||
if (isUseBackgroundFile && mfRecivedCropPicture.exists()) {
|
||||
//try {
|
||||
String filePath = utils.getBackgroundDir() + getBackgroundFileName();
|
||||
preViewFileBackgroundView = filePath;
|
||||
bvPreviewBackground.previewBackgroundImage(preViewFileBackgroundView);
|
||||
/*Drawable drawable = FileUtils.getImageDrawable(filePath);
|
||||
if (drawable != null) {
|
||||
//drawable.setAlpha(120);
|
||||
//bvPreviewBackground.setImageDrawable(drawable);
|
||||
}*/
|
||||
//ToastUtils.show("背景图片已更新");
|
||||
// } catch (IOException e) {
|
||||
// LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||
// ToastUtils.show("背景图片加载失败");
|
||||
// }
|
||||
} else {
|
||||
ToastUtils.show("未使用背景图片");
|
||||
preViewFileBackgroundView = "";
|
||||
bvPreviewBackground.previewBackgroundImage(preViewFileBackgroundView);
|
||||
// Drawable drawable = getResources().getDrawable(R.drawable.blank10x10);
|
||||
// if (drawable != null) {
|
||||
// drawable.setAlpha(120);
|
||||
// bvPreviewBackground.setImageDrawable(drawable);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
// 点击事件监听器
|
||||
private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||
bean.setIsUseBackgroundFile(false);
|
||||
utils.saveData();
|
||||
updatePreviewBackground();
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onSelectPictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (checkAndRequestStoragePermission()) {
|
||||
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
||||
startActivityForResult(intent, REQUEST_SELECT_PICTURE);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onCropPictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
File fCheck = new File(mfBackgroundDir, getBackgroundFileName());
|
||||
if (fCheck.exists()) {
|
||||
startCropImageActivity(false);
|
||||
} else {
|
||||
ToastUtils.show("没有可剪裁的图片");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onCropFreePictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
File fCheck = new File(mfBackgroundDir, getBackgroundFileName());
|
||||
if (fCheck.exists()) {
|
||||
startCropImageActivity(true);
|
||||
} else {
|
||||
ToastUtils.show("没有可剪裁的图片");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onTakePhotoClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "onTakePhotoClickListener");
|
||||
LogUtils.d(TAG, "mfTakePhoto : " + mfTakePhoto.getPath());
|
||||
|
||||
if (mfTakePhoto.exists()) {
|
||||
mfTakePhoto.delete();
|
||||
}
|
||||
try {
|
||||
mfTakePhoto.createNewFile();
|
||||
} catch (IOException e) {
|
||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||
ToastUtils.show("拍照文件创建失败");
|
||||
return;
|
||||
}
|
||||
|
||||
if (checkAndRequestStoragePermission()) {
|
||||
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
||||
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
|
||||
utils.saveData();
|
||||
updatePreviewBackground();
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onPixelPickerClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// 从文件路径启动像素拾取活动
|
||||
//String imagePath = "/storage/emulated/0/DCIM/Camera/sample.jpg";
|
||||
String imagePath = mfRecivedCropPicture.toString();
|
||||
Intent intent = new Intent(getApplicationContext(), PixelPickerActivity.class);
|
||||
intent.putExtra("imagePath", imagePath);
|
||||
startActivity(intent);
|
||||
//App.getWinBoLLActivityManager().startWinBoLLActivity(getActivity(), intent, PixelPickerActivity.class);
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||
bean.setPixelColor(0);
|
||||
utils.saveData();
|
||||
setBackgroundColor();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 压缩图片并保存到接收文件
|
||||
*/
|
||||
void compressQualityToRecivedPicture(Bitmap bitmap) {
|
||||
OutputStream outStream = null;
|
||||
try {
|
||||
mfRecivedPicture = getRecivedPictureFile(this);
|
||||
if (!mfRecivedPicture.exists()) {
|
||||
mfRecivedPicture.createNewFile();
|
||||
}
|
||||
|
||||
FileOutputStream fos = new FileOutputStream(mfRecivedPicture);
|
||||
outStream = new BufferedOutputStream(fos);
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
|
||||
outStream.flush();
|
||||
} catch (IOException e) {
|
||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||
ToastUtils.show("图片压缩失败");
|
||||
} finally {
|
||||
if (outStream != null) {
|
||||
try {
|
||||
outStream.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||
}
|
||||
}
|
||||
if (bitmap != null && !bitmap.isRecycled()) {
|
||||
bitmap.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动图片裁剪活动
|
||||
* @param isCropFree 是否自由裁剪
|
||||
*/
|
||||
public void startCropImageActivity(boolean isCropFree) {
|
||||
LogUtils.d(TAG, "startCropImageActivity");
|
||||
BackgroundPictureBean bean = mBackgroundPictureUtils.loadBackgroundPictureBean();
|
||||
mfRecivedPicture = getRecivedPictureFile(this);
|
||||
Uri uri = UriUtil.getUriForFile(this, mfRecivedPicture);
|
||||
LogUtils.d(TAG, "uri : " + uri.toString());
|
||||
|
||||
if (mfTempCropPicture.exists()) {
|
||||
mfTempCropPicture.delete();
|
||||
}
|
||||
try {
|
||||
mfTempCropPicture.createNewFile();
|
||||
} catch (IOException e) {
|
||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||
ToastUtils.show("剪裁临时文件创建失败");
|
||||
return;
|
||||
}
|
||||
|
||||
Uri cropOutPutUri = Uri.fromFile(mfTempCropPicture);
|
||||
LogUtils.d(TAG, "mfTempCropPicture : " + mfTempCropPicture.getPath());
|
||||
|
||||
Intent intent = new Intent("com.android.camera.action.CROP");
|
||||
intent.setDataAndType(uri, "image/" + _mszCommonFileType);
|
||||
intent.putExtra("crop", "true");
|
||||
intent.putExtra("noFaceDetection", true);
|
||||
|
||||
if (!isCropFree) {
|
||||
intent.putExtra("aspectX", bean.getBackgroundWidth());
|
||||
intent.putExtra("aspectY", bean.getBackgroundHeight());
|
||||
}
|
||||
|
||||
intent.putExtra("return-data", true);
|
||||
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropOutPutUri);
|
||||
intent.putExtra("scale", true);
|
||||
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
startActivityForResult(intent, REQUEST_CROP_IMAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存剪裁后的Bitmap(优化版)
|
||||
*/
|
||||
private void saveCropBitmap(Bitmap bitmap) {
|
||||
if (bitmap == null) {
|
||||
ToastUtils.show("剪裁图片为空");
|
||||
return;
|
||||
}
|
||||
|
||||
// 内存优化:大图片自动缩放
|
||||
Bitmap scaledBitmap = bitmap;
|
||||
if (bitmap.getByteCount() > 10 * 1024 * 1024) { // 超过10MB
|
||||
float scale = 1.0f;
|
||||
while (scaledBitmap.getByteCount() > 5 * 1024 * 1024) {
|
||||
scale -= 0.2f; // 每次缩小20%
|
||||
if (scale < 0.2f) break; // 最小缩放到20%
|
||||
scaledBitmap = scaleBitmap(scaledBitmap, scale);
|
||||
}
|
||||
if (scaledBitmap != bitmap) {
|
||||
bitmap.recycle(); // 回收原Bitmap
|
||||
}
|
||||
}
|
||||
|
||||
// 优化:创建保存目录
|
||||
File backgroundDir = new File(mBackgroundPictureUtils.getBackgroundDir());
|
||||
if (!backgroundDir.exists()) {
|
||||
if (!backgroundDir.mkdirs()) {
|
||||
ToastUtils.show("无法创建保存目录");
|
||||
if (scaledBitmap != bitmap) scaledBitmap.recycle();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
File saveFile = new File(backgroundDir, getBackgroundFileName());
|
||||
|
||||
// 优化:检查文件是否可写
|
||||
if (saveFile.exists() && !saveFile.canWrite()) {
|
||||
if (!saveFile.delete()) {
|
||||
ToastUtils.show("无法删除旧文件");
|
||||
if (scaledBitmap != bitmap) scaledBitmap.recycle();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(saveFile);
|
||||
boolean success = scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
|
||||
fos.flush();
|
||||
if (success) {
|
||||
ToastUtils.show("保存成功");
|
||||
// 更新数据
|
||||
mBackgroundPictureUtils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
|
||||
updatePreviewBackground();
|
||||
} else {
|
||||
ToastUtils.show("图片压缩保存失败");
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
LogUtils.e(TAG, "文件未找到" + e);
|
||||
ToastUtils.show("保存失败:文件路径错误");
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "写入异常" + e);
|
||||
ToastUtils.show("保存失败:磁盘可能已满或路径错误");
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "流关闭异常" + e);
|
||||
}
|
||||
}
|
||||
if (scaledBitmap != null && !scaledBitmap.isRecycled()) {
|
||||
scaledBitmap.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 缩放Bitmap
|
||||
*/
|
||||
private Bitmap scaleBitmap(Bitmap original, float scale) {
|
||||
if (original == null) {
|
||||
return null;
|
||||
}
|
||||
int width = (int) (original.getWidth() * scale);
|
||||
int height = (int) (original.getHeight() * scale);
|
||||
return Bitmap.createScaledBitmap(original, width, height, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分享图片
|
||||
*/
|
||||
void sharePicture() {
|
||||
Uri uri = UriUtil.getUriForFile(this, mfRecivedPicture);
|
||||
Intent shareIntent = new Intent(Intent.ACTION_SEND);
|
||||
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
|
||||
shareIntent.setType("image/" + _mszCommonFileType);
|
||||
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
startActivity(Intent.createChooser(shareIntent, "Share Image"));
|
||||
}
|
||||
|
||||
public static File getRecivedPictureFile(Context context) {
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(context);
|
||||
utils.loadBackgroundPictureBean();
|
||||
return new File(utils.getBackgroundDir(), _RecivedPictureFileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == REQUEST_SELECT_PICTURE && resultCode == RESULT_OK) {
|
||||
try {
|
||||
Uri selectedImage = data.getData();
|
||||
LogUtils.d(TAG, "Uri is : " + selectedImage.toString());
|
||||
File fSrcImage = new File(UriUtil.getFilePathFromUri(this, selectedImage));
|
||||
mfRecivedPicture = getRecivedPictureFile(this);
|
||||
if (FileUtils.copyFile(fSrcImage, mfRecivedPicture)) {
|
||||
startCropImageActivity(false);
|
||||
} else {
|
||||
ToastUtils.show("图片复制失败,请重试");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "选择图片异常" + e);
|
||||
ToastUtils.show("选择图片失败:" + e.getMessage());
|
||||
}
|
||||
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
|
||||
LogUtils.d(TAG, "REQUEST_TAKE_PHOTO");
|
||||
Bundle extras = data.getExtras();
|
||||
if (extras != null) {
|
||||
Bitmap imageBitmap = (Bitmap) extras.get("data");
|
||||
if (imageBitmap != null) {
|
||||
compressQualityToRecivedPicture(imageBitmap);
|
||||
startCropImageActivity(false);
|
||||
} else {
|
||||
ToastUtils.show("拍照图片为空");
|
||||
}
|
||||
} else {
|
||||
ToastUtils.show("拍照数据获取失败");
|
||||
}
|
||||
} else if (requestCode == REQUEST_CROP_IMAGE && resultCode == RESULT_OK) {
|
||||
LogUtils.d(TAG, "CROP_IMAGE_REQUEST_CODE");
|
||||
try {
|
||||
Bitmap cropBitmap = null;
|
||||
// 方案1:通过Intent获取剪裁后的Bitmap
|
||||
if (data != null && data.hasExtra("data")) {
|
||||
cropBitmap = data.getParcelableExtra("data");
|
||||
} else if (mfTempCropPicture.exists()) {
|
||||
cropBitmap = BitmapFactory.decodeFile(mfTempCropPicture.getPath());
|
||||
} else {
|
||||
ToastUtils.show("剪裁文件不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
if (cropBitmap != null) {
|
||||
saveCropBitmap(cropBitmap);
|
||||
} else {
|
||||
ToastUtils.show("获取剪裁图片失败");
|
||||
}
|
||||
} catch (OutOfMemoryError e) {
|
||||
LogUtils.e(TAG, "内存溢出" + e);
|
||||
ToastUtils.show("保存失败:内存不足,请尝试裁剪更小的图片");
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "剪裁保存异常" + e);
|
||||
ToastUtils.show("保存失败:" + e.getMessage());
|
||||
}/* finally {
|
||||
// 安全删除临时文件
|
||||
if (mfTempCropPicture.exists()) {
|
||||
mfTempCropPicture.delete();
|
||||
}
|
||||
}*/
|
||||
} else if (resultCode != RESULT_OK) {
|
||||
LogUtils.d(TAG, "操作取消或失败,requestCode: " + requestCode);
|
||||
ToastUtils.show("操作已取消");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查类型是否为图片
|
||||
*/
|
||||
private boolean isImageType(String type) {
|
||||
return type.startsWith("image/") || "image/jpeg".equals(type) ||
|
||||
"image/jpg".equals(type) || "image/png".equals(type) ||
|
||||
"image/webp".equals(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查并申请存储权限
|
||||
*/
|
||||
private boolean checkAndRequestStoragePermission() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
||||
STORAGE_PERMISSION_REQUEST);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == STORAGE_PERMISSION_REQUEST) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
ToastUtils.show("存储权限已获取");
|
||||
} else {
|
||||
ToastUtils.show("需要存储权限才能保存图片");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setBackgroundColor() {
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
|
||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||
int nPixelColor = bean.getPixelColor();
|
||||
RelativeLayout mainLayout = findViewById(R.id.activitybackgroundpictureRelativeLayout1);
|
||||
mainLayout.setBackgroundColor(nPixelColor);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
setBackgroundColor();
|
||||
}
|
||||
|
||||
public void onNetworkBackgroundDialog(View view) {
|
||||
// 在需要显示对话框的地方(如网络状态监听回调中)
|
||||
NetworkBackgroundDialog dialog = new NetworkBackgroundDialog(this, new NetworkBackgroundDialog.OnDialogClickListener() {
|
||||
@Override
|
||||
public void onConfirm() {
|
||||
ToastUtils.show("onConfirm");
|
||||
// 处理确认逻辑(如允许后台网络使用)
|
||||
LogUtils.d("MainActivity", "用户允许后台网络使用");
|
||||
// 执行具体业务:如开启后台网络请求服务
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
ToastUtils.show("onCancel");
|
||||
// 处理取消逻辑(如禁止后台网络使用)
|
||||
LogUtils.d("MainActivity", "用户禁止后台网络使用");
|
||||
// 执行具体业务:如关闭后台网络请求
|
||||
}
|
||||
});
|
||||
|
||||
// 可选:修改对话框标题和内容(适配自定义场景)
|
||||
dialog.setTitle("网络图片下载对话框");
|
||||
dialog.setContent("是否下载地址中的图片资源,作为应用背景图片?");
|
||||
|
||||
// 显示对话框
|
||||
dialog.show();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写finish方法,确保所有退出场景都触发Toast
|
||||
*/
|
||||
@Override
|
||||
public void finish() {
|
||||
if (!isCommitSettings) {
|
||||
YesNoAlertDialog.show(this, "应用背景更改提示:", "是否应用预览图片?", new YesNoAlertDialog.OnDialogResultListener(){
|
||||
|
||||
@Override
|
||||
public void onNo() {
|
||||
isCommitSettings = true;
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onYes() {
|
||||
bvPreviewBackground.saveToBackgroundSources(preViewFileBackgroundView);
|
||||
isCommitSettings = true;
|
||||
finish();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
super.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,806 +0,0 @@
|
||||
package cc.winboll.studio.powerbell.activities;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Bitmap.CompressFormat;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.MediaStore;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.content.FileProvider;
|
||||
import cc.winboll.studio.libaes.views.AToolbar;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.App;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.dialogs.BackgroundPicturePreviewDialog;
|
||||
import cc.winboll.studio.powerbell.dialogs.YesNoAlertDialog;
|
||||
import cc.winboll.studio.powerbell.model.BackgroundBean;
|
||||
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||
import cc.winboll.studio.powerbell.utils.ImageCropUtils;
|
||||
import cc.winboll.studio.powerbell.utils.PermissionUtils;
|
||||
import cc.winboll.studio.powerbell.views.BackgroundView;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import cc.winboll.studio.powerbell.utils.UriUtil;
|
||||
import cc.winboll.studio.powerbell.utils.FileUtils;
|
||||
|
||||
public class BackgroundSettingsActivity extends WinBoLLActivity implements BackgroundPicturePreviewDialog.IOnRecivedPictureListener {
|
||||
|
||||
public static final String TAG = "BackgroundSettingsActivity";
|
||||
private BackgroundSourceUtils mBgSourceUtils;
|
||||
private PermissionUtils mPermissionUtils;
|
||||
|
||||
public static final int REQUEST_SELECT_PICTURE = 0;
|
||||
public static final int REQUEST_TAKE_PHOTO = 1;
|
||||
public static final int REQUEST_CROP_IMAGE = 2;
|
||||
|
||||
private Toolbar mToolbar;
|
||||
private BackgroundView mBackgroundView;
|
||||
private File mfTakePhoto;
|
||||
volatile boolean isCommitSettings = false;
|
||||
volatile boolean isPreviewBackgroundChanged = false;
|
||||
|
||||
@Override
|
||||
public Activity getActivity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_background_settings);
|
||||
|
||||
mBackgroundView = (BackgroundView) findViewById(R.id.background_view);
|
||||
mBgSourceUtils = BackgroundSourceUtils.getInstance(this);
|
||||
mBgSourceUtils.loadSettings();
|
||||
mPermissionUtils = PermissionUtils.getInstance();
|
||||
|
||||
// File tempDir = new File(App.getTempDirPath());
|
||||
// if (!tempDir.exists()) {
|
||||
// tempDir.mkdirs();
|
||||
// }
|
||||
// mfTakePhoto = new File(tempDir, "TakePhoto.jpg");
|
||||
//
|
||||
// File selectTempDir = new File(mBgSourceUtils.getBackgroundSourceDirPath(), "SelectTemp");
|
||||
// if (!selectTempDir.exists()) {
|
||||
// selectTempDir.mkdirs();
|
||||
// LogUtils.d(TAG, "【选图初始化】选图临时目录创建完成:" + selectTempDir.getAbsolutePath());
|
||||
// }
|
||||
|
||||
initToolbar();
|
||||
initClickListeners();
|
||||
|
||||
if (handleShareIntent()) {
|
||||
ToastUtils.show("handleShareIntent");
|
||||
} else {
|
||||
mBgSourceUtils.setCurrentSourceToPreview();
|
||||
}
|
||||
|
||||
Uri uriSelectedImage = UriUtil.getUriForFile(this, new File(mBgSourceUtils.getPreviewBackgroundBean().getBackgroundFilePath()));
|
||||
// 创建预览数据剪裁环境
|
||||
mBgSourceUtils.createAndUpdatePreviewEnvironmentForCropping(mBgSourceUtils.getPreviewBackgroundBean());
|
||||
doubleRefreshPreview();
|
||||
LogUtils.d(TAG, "【初始化】BackgroundSettingsActivity 初始化完成");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostCreate(Bundle savedInstanceState) {
|
||||
super.onPostCreate(savedInstanceState);
|
||||
doubleRefreshPreview();
|
||||
}
|
||||
|
||||
private void initToolbar() {
|
||||
mToolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(mToolbar);
|
||||
mToolbar.setSubtitle(getTag());
|
||||
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【导航栏】点击返回");
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initClickListeners() {
|
||||
findViewById(R.id.activitybackgroundpictureAButton5).setOnClickListener(onOriginNullClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton4).setOnClickListener(onReceivedPictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton1).setOnClickListener(onTakePhotoClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton2).setOnClickListener(onSelectPictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton3).setOnClickListener(onCropPictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton6).setOnClickListener(onCropFreePictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton7).setOnClickListener(onPixelPickerClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton8).setOnClickListener(onCleanPixelClickListener);
|
||||
}
|
||||
|
||||
private boolean handleShareIntent() {
|
||||
Intent intent = getIntent();
|
||||
if (intent != null) {
|
||||
String action = intent.getAction();
|
||||
String type = intent.getType();
|
||||
if (Intent.ACTION_SEND.equals(action) && type != null && isImageType(type)) {
|
||||
BackgroundPicturePreviewDialog dlg = new BackgroundPicturePreviewDialog(this);
|
||||
dlg.show();
|
||||
LogUtils.d(TAG, "【分享处理】收到分享图片意图");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean isImageType(String lowerMimeType) {
|
||||
return lowerMimeType.equals("image/jpeg")
|
||||
|| lowerMimeType.equals("image/png")
|
||||
|| lowerMimeType.equals("image/tiff")
|
||||
|| lowerMimeType.equals("image/jpg")
|
||||
|| lowerMimeType.equals("image/svg+xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAcceptRecivedPicture(String szPreRecivedPictureName) {
|
||||
ToastUtils.show("图片接收功能暂未实现");
|
||||
LogUtils.d(TAG, "【分享接收】图片名:" + szPreRecivedPictureName);
|
||||
}
|
||||
|
||||
private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】取消背景图片");
|
||||
BackgroundBean previewBackgroundBean = mBgSourceUtils.getPreviewBackgroundBean();
|
||||
previewBackgroundBean.setIsUseBackgroundFile(false);
|
||||
doubleRefreshPreview();
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onSelectPictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】选择图片");
|
||||
if (mPermissionUtils.checkAndRequestStoragePermission(BackgroundSettingsActivity.this)) {
|
||||
LogUtils.d(TAG, "【选图权限】已获取");
|
||||
Intent[] intents = new Intent[3];
|
||||
Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
getContentIntent.setType("image/*");
|
||||
getContentIntent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
getContentIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intents[0] = getContentIntent;
|
||||
|
||||
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
||||
pickIntent.setType("image/*");
|
||||
pickIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intents[1] = pickIntent;
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
Intent openDocIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
openDocIntent.setType("image/*");
|
||||
openDocIntent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
openDocIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
|
||||
intents[2] = openDocIntent;
|
||||
}
|
||||
|
||||
Intent validIntent = null;
|
||||
for (int i = 0; i < intents.length; i++) {
|
||||
Intent intent = intents[i];
|
||||
if (intent != null && intent.resolveActivity(getPackageManager()) != null) {
|
||||
validIntent = intent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (validIntent != null) {
|
||||
Intent chooser = Intent.createChooser(validIntent, "选择图片");
|
||||
chooser.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
|
||||
startActivityForResult(chooser, REQUEST_SELECT_PICTURE);
|
||||
LogUtils.d(TAG, "【选图意图】启动图片选择");
|
||||
} else {
|
||||
LogUtils.d(TAG, "【选图意图】无相册应用");
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ToastUtils.show("未找到相册应用,请安装后重试");
|
||||
new AlertDialog.Builder(BackgroundSettingsActivity.this)
|
||||
.setTitle("无图片选择应用")
|
||||
.setMessage("需要安装相册应用才能选择图片")
|
||||
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
|
||||
marketIntent.setData(Uri.parse("market://details?id=com.android.gallery3d"));
|
||||
if (marketIntent.resolveActivity(getPackageManager()) != null) {
|
||||
startActivity(marketIntent);
|
||||
} else {
|
||||
ToastUtils.show("无法打开应用商店");
|
||||
}
|
||||
}
|
||||
})
|
||||
.setNegativeButton("取消", null)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
LogUtils.d(TAG, "【选图权限】已申请");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onCropPictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】固定比例裁剪");
|
||||
// 调用裁剪工具类:传入上下文、预览图、固定比例(按视图宽高)、请求码
|
||||
ImageCropUtils.startImageCrop(BackgroundSettingsActivity.this,
|
||||
mBgSourceUtils.getPreviewBackgroundBean(),
|
||||
mBackgroundView.getWidth(),
|
||||
mBackgroundView.getHeight(),
|
||||
false,
|
||||
REQUEST_CROP_IMAGE
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onCropFreePictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】自由裁剪");
|
||||
// 调用裁剪工具类:传入上下文、预览图、自由裁剪(比例参数传0)、请求码
|
||||
ImageCropUtils.startImageCrop(BackgroundSettingsActivity.this,
|
||||
mBgSourceUtils.getPreviewBackgroundBean(),
|
||||
0,
|
||||
0,
|
||||
true,
|
||||
REQUEST_CROP_IMAGE
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onTakePhotoClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】拍照");
|
||||
if (mfTakePhoto.exists()) {
|
||||
boolean deleteSuccess = mfTakePhoto.delete();
|
||||
LogUtils.d(TAG, "【拍照准备】清理旧文件:" + (deleteSuccess ? "成功" : "失败"));
|
||||
}
|
||||
try {
|
||||
boolean createSuccess = mfTakePhoto.createNewFile();
|
||||
LogUtils.d(TAG, "【拍照准备】创建新文件:" + (createSuccess ? "成功" : "失败"));
|
||||
if (!createSuccess) {
|
||||
ToastUtils.show("拍照文件创建失败");
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【拍照异常】" + e.getMessage());
|
||||
ToastUtils.show("拍照文件创建失败");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mPermissionUtils.checkAndRequestStoragePermission(BackgroundSettingsActivity.this)) {
|
||||
LogUtils.d(TAG, "【拍照权限】已获取");
|
||||
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
try {
|
||||
Uri photoUri = getFileProviderUri(mfTakePhoto);
|
||||
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
|
||||
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
|
||||
LogUtils.d(TAG, "【拍照启动】Uri:" + photoUri.toString());
|
||||
} catch (Exception e) {
|
||||
String errMsg = "拍照启动异常:" + e.getMessage();
|
||||
ToastUtils.show(errMsg.substring(0, 20));
|
||||
LogUtils.e(TAG, "【拍照失败】");
|
||||
}
|
||||
} else {
|
||||
LogUtils.d(TAG, "【拍照权限】已申请");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
ToastUtils.show("图片接收功能暂未实现");
|
||||
LogUtils.d(TAG, "【按钮点击】图片接收");
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onPixelPickerClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】像素拾取");
|
||||
String targetImagePath = mBgSourceUtils.getCurrentBackgroundFilePath();
|
||||
File targetFile = new File(targetImagePath);
|
||||
if (targetFile == null || !targetFile.exists() || targetFile.length() <= 0) {
|
||||
ToastUtils.show("无有效图片可拾取像素");
|
||||
LogUtils.e(TAG, "【像素拾取失败】");
|
||||
return;
|
||||
}
|
||||
Intent intent = new Intent(getApplicationContext(), PixelPickerActivity.class);
|
||||
intent.putExtra("imagePath", targetImagePath);
|
||||
startActivity(intent);
|
||||
LogUtils.d(TAG, "【像素拾取启动】");
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】清空像素颜色");
|
||||
BackgroundBean bean = mBgSourceUtils.getCurrentBackgroundBean();
|
||||
int oldColor = bean.getPixelColor();
|
||||
bean.setPixelColor(0);
|
||||
mBgSourceUtils.saveSettings();
|
||||
doubleRefreshPreview();
|
||||
ToastUtils.show("像素颜色已清空");
|
||||
LogUtils.d(TAG, "【像素清空】旧颜色:" + oldColor);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
LogUtils.d(TAG, "【回调触发】requestCode:" + requestCode + ",resultCode:" + resultCode);
|
||||
|
||||
try {
|
||||
if (requestCode == PermissionUtils.REQUEST_MANAGE_EXTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
handleStoragePermissionCallback();
|
||||
return;
|
||||
}
|
||||
|
||||
if (resultCode != RESULT_OK) {
|
||||
handleOperationCancelOrFail();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (requestCode) {
|
||||
case REQUEST_SELECT_PICTURE:
|
||||
handleSelectPictureResult(resultCode, data);
|
||||
break;
|
||||
case REQUEST_TAKE_PHOTO:
|
||||
handleTakePhotoResult(resultCode, data);
|
||||
break;
|
||||
case REQUEST_CROP_IMAGE:
|
||||
handleCropImageResult(requestCode, resultCode, data);
|
||||
break;
|
||||
default:
|
||||
LogUtils.d(TAG, "【回调忽略】未知requestCode");
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "【回调异常】" + e.getMessage());
|
||||
ToastUtils.show("操作失败");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleStoragePermissionCallback() {
|
||||
if (Environment.isExternalStorageManager()) {
|
||||
LogUtils.d(TAG, "【权限回调】已授予");
|
||||
ToastUtils.show("存储权限已获取");
|
||||
} else {
|
||||
LogUtils.d(TAG, "【权限回调】已拒绝");
|
||||
ToastUtils.show("存储权限不足");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleTakePhotoResult(int resultCode, Intent data) {
|
||||
if (resultCode != RESULT_OK || data == null) {
|
||||
handleOperationCancelOrFail();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mfTakePhoto.exists() || mfTakePhoto.length() <= 0) {
|
||||
ToastUtils.show("拍照文件无效");
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap photoBitmap = getTakePhotoBitmap(data);
|
||||
if (photoBitmap != null && !photoBitmap.isRecycled()) {
|
||||
mBgSourceUtils.compressQualityToRecivedPicture(photoBitmap);
|
||||
} else {
|
||||
ToastUtils.show("拍照图片为空");
|
||||
return;
|
||||
}
|
||||
|
||||
mBgSourceUtils.saveFileToPreviewBean(mfTakePhoto, mfTakePhoto.getAbsolutePath());
|
||||
doubleRefreshPreview();
|
||||
|
||||
// 拍照后启动固定比例裁剪(调用工具类)
|
||||
ImageCropUtils.startImageCrop(BackgroundSettingsActivity.this,
|
||||
mBgSourceUtils.getPreviewBackgroundBean(),
|
||||
mBackgroundView.getWidth(),
|
||||
mBackgroundView.getHeight(),
|
||||
false,
|
||||
REQUEST_CROP_IMAGE
|
||||
);
|
||||
LogUtils.d(TAG, "【拍照完成】已启动裁剪");
|
||||
}
|
||||
|
||||
private Bitmap getTakePhotoBitmap(Intent data) {
|
||||
LogUtils.d(TAG, "【拍照Bitmap解析】开始");
|
||||
if (mfTakePhoto != null && mfTakePhoto.exists()) {
|
||||
LogUtils.d(TAG, "【拍照Bitmap解析】从文件解析");
|
||||
Bitmap photoBitmap = parseCropTempFileToBitmap(mfTakePhoto);
|
||||
if (photoBitmap != null && !photoBitmap.isRecycled()) {
|
||||
LogUtils.d(TAG, "【拍照Bitmap解析】成功");
|
||||
return photoBitmap;
|
||||
} else {
|
||||
LogUtils.w(TAG, "【拍照Bitmap解析】文件解析失败,尝试Intent");
|
||||
}
|
||||
} else {
|
||||
LogUtils.w(TAG, "【拍照Bitmap解析】文件无效,尝试Intent");
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
try {
|
||||
Bitmap thumbnailBitmap = (Bitmap) data.getParcelableExtra("data");
|
||||
if (thumbnailBitmap != null && !thumbnailBitmap.isRecycled()) {
|
||||
LogUtils.d(TAG, "【拍照Bitmap解析】从Intent获取成功");
|
||||
return thumbnailBitmap;
|
||||
} else {
|
||||
LogUtils.e(TAG, "【拍照Bitmap解析】Intent解析失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "【拍照Bitmap解析】Intent异常:" + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
LogUtils.e(TAG, "【拍照Bitmap解析】Intent为空");
|
||||
}
|
||||
|
||||
LogUtils.e(TAG, "【拍照Bitmap解析】失败");
|
||||
ToastUtils.show("拍照图片解析失败");
|
||||
return null;
|
||||
}
|
||||
|
||||
private void handleSelectPictureResult(int resultCode, Intent data) {
|
||||
if (resultCode != RESULT_OK || data == null) {
|
||||
handleOperationCancelOrFail();
|
||||
return;
|
||||
}
|
||||
|
||||
Uri selectedImage = data.getData();
|
||||
if (selectedImage == null) {
|
||||
ToastUtils.show("图片Uri为空");
|
||||
return;
|
||||
}
|
||||
LogUtils.d(TAG, "【选图回调】Uri : " + selectedImage.toString());
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
getContentResolver().takePersistableUriPermission(
|
||||
selectedImage,
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
);
|
||||
LogUtils.d(TAG, "【选图权限】已添加持久化权限");
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, "【选图同步】路径绑定完成");
|
||||
// 选图后启动固定比例裁剪(调用工具类)
|
||||
putUriFileToPreviewSource(selectedImage);
|
||||
ImageCropUtils.startImageCrop(BackgroundSettingsActivity.this,
|
||||
mBgSourceUtils.getPreviewBackgroundBean(),
|
||||
mBackgroundView.getWidth(),
|
||||
mBackgroundView.getHeight(),
|
||||
false,
|
||||
REQUEST_CROP_IMAGE
|
||||
);
|
||||
}
|
||||
|
||||
boolean putUriFileToPreviewSource(Uri srcUriFile) {
|
||||
File srcFile = new File(UriUtil.getFilePathFromUri(this, srcUriFile));
|
||||
return putUriFileToPreviewSource(srcFile);
|
||||
}
|
||||
|
||||
boolean putUriFileToPreviewSource(File srcFile) {
|
||||
mBgSourceUtils.loadSettings();
|
||||
File dstFile = new File(mBgSourceUtils.getPreviewBackgroundBean().getBackgroundFilePath());
|
||||
return FileUtils.copyFile(srcFile, dstFile);
|
||||
}
|
||||
|
||||
private void handleCropImageResult(int requestCode, int resultCode, Intent data) {
|
||||
LogUtils.d(TAG, "handleCropImageResult: 处理裁剪结果");
|
||||
File cropTempFile = new File(mBgSourceUtils.getPreviewBackgroundBean().getBackgroundScaledCompressFilePath());
|
||||
boolean isFileExist = cropTempFile.exists();
|
||||
boolean isFileReadable = isFileExist ? cropTempFile.canRead() : false;
|
||||
long fileSize = isFileExist ? cropTempFile.length() : 0;
|
||||
boolean isCropSuccess = (resultCode == RESULT_OK) && isFileExist && isFileReadable && fileSize > 100;
|
||||
|
||||
if (isCropSuccess) {
|
||||
isPreviewBackgroundChanged = true;
|
||||
LogUtils.d(TAG, "handleCropImageResult: 裁剪成功");
|
||||
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
|
||||
previewBean.setIsUseBackgroundFile(true);
|
||||
previewBean.setIsUseBackgroundScaledCompressFile(true);
|
||||
mBgSourceUtils.saveSettings();
|
||||
|
||||
float systemFileRatio = getRatioFromSystemCropFile(cropTempFile);
|
||||
if (systemFileRatio > 0) {
|
||||
Bitmap cropBitmap = parseCropTempFileToBitmap(cropTempFile);
|
||||
if (cropBitmap != null && !cropBitmap.isRecycled()) {
|
||||
Bitmap scaledCropBitmap = adjustBitmapToFinalRatio(cropBitmap, systemFileRatio);
|
||||
saveScaledBitmapToFile(scaledCropBitmap, cropTempFile);
|
||||
|
||||
if (scaledCropBitmap != cropBitmap && !scaledCropBitmap.isRecycled()) {
|
||||
scaledCropBitmap.recycle();
|
||||
}
|
||||
if (!cropBitmap.isRecycled()) {
|
||||
cropBitmap.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (mBackgroundView != null && !isFinishing()) {
|
||||
mBackgroundView.loadBackgroundBean(mBgSourceUtils.getPreviewBackgroundBean());
|
||||
LogUtils.d(TAG, "handleCropImageResult: 裁剪图片加载完成");
|
||||
}
|
||||
}
|
||||
}, 50);
|
||||
} else {
|
||||
handleOperationCancelOrFail();
|
||||
}
|
||||
}
|
||||
|
||||
private float getRatioFromSystemCropFile(File systemCropFile) {
|
||||
LogUtils.d(TAG, "getRatioFromSystemCropFile: 读取比例");
|
||||
if (systemCropFile == null || !systemCropFile.exists() || !systemCropFile.isFile()) {
|
||||
LogUtils.e(TAG, "getRatioFromSystemCropFile: 文件无效");
|
||||
return -1;
|
||||
}
|
||||
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFile(systemCropFile.getAbsolutePath(), options);
|
||||
|
||||
int cropWidth = options.outWidth;
|
||||
int cropHeight = options.outHeight;
|
||||
if (cropWidth <= 0 || cropHeight <= 0) {
|
||||
LogUtils.e(TAG, "getRatioFromSystemCropFile: 尺寸无效");
|
||||
return -1;
|
||||
}
|
||||
|
||||
float systemRatio = (float) cropWidth / cropHeight;
|
||||
LogUtils.d(TAG, "getRatioFromSystemCropFile: 比例:" + systemRatio);
|
||||
return systemRatio;
|
||||
}
|
||||
|
||||
private Bitmap adjustBitmapToFinalRatio(Bitmap originalBitmap, float finalCropRatio) {
|
||||
LogUtils.d(TAG, "adjustBitmapToFinalRatio: 调整比例");
|
||||
if (originalBitmap == null || originalBitmap.isRecycled() || finalCropRatio <= 0) {
|
||||
LogUtils.e(TAG, "adjustBitmapToFinalRatio: 参数无效");
|
||||
return originalBitmap;
|
||||
}
|
||||
|
||||
int originalWidth = originalBitmap.getWidth();
|
||||
int originalHeight = originalBitmap.getHeight();
|
||||
float originalRatio = (float) originalWidth / originalHeight;
|
||||
|
||||
if (Math.abs(originalRatio - finalCropRatio) < 0.001f) {
|
||||
LogUtils.d(TAG, "adjustBitmapToFinalRatio: 比例一致");
|
||||
return originalBitmap;
|
||||
}
|
||||
|
||||
int targetWidth, targetHeight;
|
||||
targetHeight = originalHeight;
|
||||
targetWidth = Math.round(targetHeight * finalCropRatio);
|
||||
if (targetWidth > originalWidth) {
|
||||
targetWidth = originalWidth;
|
||||
targetHeight = Math.round(targetWidth / finalCropRatio);
|
||||
}
|
||||
|
||||
targetWidth = Math.round(targetHeight * finalCropRatio);
|
||||
LogUtils.d(TAG, "adjustBitmapToFinalRatio: 调整前:" + originalWidth + "x" + originalHeight + ",调整后:" + targetWidth + "x" + targetHeight);
|
||||
|
||||
Bitmap adjustedBitmap = Bitmap.createBitmap(
|
||||
originalBitmap,
|
||||
(originalWidth - targetWidth) / 2,
|
||||
(originalHeight - targetHeight) / 2,
|
||||
targetWidth,
|
||||
targetHeight
|
||||
);
|
||||
|
||||
return adjustedBitmap;
|
||||
}
|
||||
|
||||
private void saveScaledBitmapToFile(Bitmap bitmap, File targetFile) {
|
||||
LogUtils.d(TAG, "saveScaledBitmapToFile: 保存图片");
|
||||
if (bitmap == null || bitmap.isRecycled() || targetFile == null) {
|
||||
LogUtils.e(TAG, "saveScaledBitmapToFile: 参数无效");
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetFile.exists()) {
|
||||
boolean deleteSuccess = targetFile.delete();
|
||||
LogUtils.d(TAG, "saveScaledBitmapToFile: 删除原文件:" + (deleteSuccess ? "成功" : "失败"));
|
||||
}
|
||||
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
|
||||
bitmap.compress(CompressFormat.JPEG, 100, outputStream);
|
||||
outputStream.flush();
|
||||
LogUtils.d(TAG, "saveScaledBitmapToFile: 保存成功");
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "saveScaledBitmapToFile: 异常:" + e.getMessage());
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "saveScaledBitmapToFile: 关闭流异常");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Bitmap parseCropTempFileToBitmap(File cropTempFile) {
|
||||
LogUtils.d(TAG, "parseCropTempFileToBitmap: 解析文件");
|
||||
if (cropTempFile == null || !cropTempFile.exists() || !cropTempFile.isFile() || cropTempFile.length() <= 100) {
|
||||
LogUtils.e(TAG, "parseCropTempFileToBitmap: 文件无效");
|
||||
return null;
|
||||
}
|
||||
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFile(cropTempFile.getAbsolutePath(), options);
|
||||
|
||||
int maxSize = 2048;
|
||||
int sampleSize = 1;
|
||||
while (options.outWidth / sampleSize > maxSize || options.outHeight / sampleSize > maxSize) {
|
||||
sampleSize *= 2;
|
||||
}
|
||||
sampleSize = Math.min(sampleSize, 16);
|
||||
LogUtils.d(TAG, "parseCropTempFileToBitmap: 采样率:" + sampleSize);
|
||||
|
||||
options.inJustDecodeBounds = false;
|
||||
options.inSampleSize = sampleSize;
|
||||
options.inPreferredConfig = Bitmap.Config.RGB_565;
|
||||
options.inPurgeable = true;
|
||||
options.inInputShareable = true;
|
||||
|
||||
Bitmap cropBitmap = null;
|
||||
try {
|
||||
cropBitmap = BitmapFactory.decodeFile(cropTempFile.getAbsolutePath(), options);
|
||||
if (cropBitmap == null || cropBitmap.isRecycled()) {
|
||||
LogUtils.e(TAG, "parseCropTempFileToBitmap: 解析失败");
|
||||
return null;
|
||||
}
|
||||
LogUtils.d(TAG, "parseCropTempFileToBitmap: 解析成功");
|
||||
} catch (OutOfMemoryError e) {
|
||||
LogUtils.e(TAG, "parseCropTempFileToBitmap: OOM");
|
||||
Toast.makeText(this, "图片解析失败", Toast.LENGTH_SHORT).show();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "parseCropTempFileToBitmap: 异常:" + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
return cropBitmap;
|
||||
}
|
||||
|
||||
private void doubleRefreshPreview() {
|
||||
LogUtils.d(TAG, "【双重刷新】开始");
|
||||
if (mBackgroundView != null && !isFinishing()) {
|
||||
mBgSourceUtils.loadSettings();
|
||||
mBackgroundView.loadBackgroundBean(mBgSourceUtils.getPreviewBackgroundBean());
|
||||
LogUtils.d(TAG, "【双重刷新】第一重完成");
|
||||
} else {
|
||||
LogUtils.w(TAG, "【双重刷新】跳过");
|
||||
return;
|
||||
}
|
||||
|
||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (mBackgroundView != null && !isFinishing()) {
|
||||
mBgSourceUtils.loadSettings();
|
||||
mBackgroundView.loadBackgroundBean(mBgSourceUtils.getPreviewBackgroundBean());
|
||||
LogUtils.d(TAG, "【双重刷新】第二重完成");
|
||||
}
|
||||
}
|
||||
}, 50);
|
||||
}
|
||||
|
||||
|
||||
private void handleOperationCancelOrFail() {
|
||||
mBgSourceUtils.setCurrentSourceToPreview();
|
||||
LogUtils.d(TAG, "【操作回调】取消或失败");
|
||||
ToastUtils.show("【操作回调】取消或失败");
|
||||
doubleRefreshPreview();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 启动系统裁剪工具
|
||||
* @param activity 上下文
|
||||
* @param srcFile 裁剪原图
|
||||
* @param aspectX 裁剪宽比例(自由裁剪传0)
|
||||
* @param aspectY 裁剪高比例(自由裁剪传0)
|
||||
* @param isFreeCrop 是否自由裁剪
|
||||
* @param requestCode 裁剪请求码
|
||||
*/
|
||||
/**
|
||||
* 启动系统裁剪工具
|
||||
* @param activity 上下文
|
||||
* @param srcFile 裁剪原图
|
||||
* @param aspectX 裁剪宽比例(自由裁剪传0)
|
||||
* @param aspectY 裁剪高比例(自由裁剪传0)
|
||||
* @param isFreeCrop 是否自由裁剪
|
||||
* @param requestCode 裁剪请求码
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* 获取FileProvider Uri(复用方法,避免重复代码)
|
||||
*/
|
||||
public Uri getFileProviderUri(File file) {
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
String FILE_PROVIDER_AUTHORITY = getPackageName() + ".fileprovider";
|
||||
|
||||
return FileProvider.getUriForFile(this, FILE_PROVIDER_AUTHORITY, file);
|
||||
} else {
|
||||
return Uri.fromFile(file);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "getFileProviderUri: 生成Uri失败:" + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
LogUtils.d(TAG, "【权限回调】转发处理");
|
||||
mPermissionUtils.handleStoragePermissionResult(this, requestCode, permissions, grantResults);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
if (isCommitSettings) {
|
||||
super.finish();
|
||||
} else {
|
||||
// 如果预览背景改变过就提示是否更换背景
|
||||
if (isPreviewBackgroundChanged) {
|
||||
YesNoAlertDialog.show(this, "背景更换问题", "是否确定背景图片设置?", new YesNoAlertDialog.OnDialogResultListener(){
|
||||
@Override
|
||||
public void onYes() {
|
||||
mBgSourceUtils.commitPreviewSourceToCurrent();
|
||||
isCommitSettings = true;
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNo() {
|
||||
isCommitSettings = true;
|
||||
finish();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 如果预览背景未改变就直接退出
|
||||
isCommitSettings = true;
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,8 @@ import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -35,11 +32,11 @@ import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
|
||||
public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
public class BatteryReportActivity extends Activity {
|
||||
public static final String TAG = "BatteryReportActivity";
|
||||
|
||||
private Toolbar mToolbar;
|
||||
private RecyclerView rvBatteryReport;
|
||||
private BatteryReportAdapter adapter;
|
||||
private List<AppBatteryModel> dataList = new ArrayList<AppBatteryModel>();
|
||||
@@ -53,34 +50,10 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
|
||||
private Map<String, String> packageToAppNameCache = new HashMap<String, String>();
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
@Override
|
||||
public Activity getActivity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_battery_report);
|
||||
|
||||
mToolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(mToolbar);
|
||||
mToolbar.setSubtitle(getTag());
|
||||
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【导航栏】点击返回");
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
mPackageManager = getPackageManager();
|
||||
|
||||
// 权限检查(Java7 传统条件判断)
|
||||
|
||||
@@ -7,37 +7,26 @@ import android.view.View;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
import cc.winboll.studio.libaes.views.AOHPCTCSeekBar;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import cc.winboll.studio.libaes.views.AToolbar;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.App;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.model.BatteryInfoBean;
|
||||
import cc.winboll.studio.powerbell.beans.BatteryInfoBean;
|
||||
import cc.winboll.studio.powerbell.receivers.ControlCenterServiceReceiver;
|
||||
import cc.winboll.studio.powerbell.utils.AppCacheUtils;
|
||||
import cc.winboll.studio.powerbell.utils.StringUtils;
|
||||
import java.util.ArrayList;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
|
||||
public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
public class ClearRecordActivity extends Activity {
|
||||
|
||||
public static final String TAG = "ClearRecordActivity";
|
||||
|
||||
private Toolbar mToolbar;
|
||||
AToolbar mAToolbar;
|
||||
TextView mtvRecordText;
|
||||
App mApplication;
|
||||
boolean mIsShowRecordWithEnter = false;
|
||||
|
||||
@Override
|
||||
public Activity getActivity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -45,18 +34,21 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
|
||||
mApplication = (App) getApplication();
|
||||
|
||||
// 初始化工具栏
|
||||
mToolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(mToolbar);
|
||||
mToolbar.setSubtitle(getTag());
|
||||
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【导航栏】点击返回");
|
||||
finish();
|
||||
}
|
||||
});
|
||||
mAToolbar = (AToolbar) findViewById(R.id.toolbar);
|
||||
setActionBar(mAToolbar);
|
||||
//mAToolbar.setTitle(getTitle() + " - " + getString(R.string.subtitle_activity_clearrecord));
|
||||
mAToolbar.setSubtitle(R.string.subtitle_activity_clearrecord);
|
||||
//mAToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
|
||||
//mAToolbar.setSubtitleTextAppearance(this, R.style.Toolbar_SubTitleText);
|
||||
//mAToolbar.setBackgroundColor(getColor(R.color.colorPrimary));
|
||||
setActionBar(mAToolbar);
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mAToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
// 设置滑动清理控件
|
||||
//
|
||||
|
||||
@@ -24,10 +24,10 @@ import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libaes.views.AToolbar;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.activities.BackgroundSettingsActivity;
|
||||
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
|
||||
import cc.winboll.studio.powerbell.activities.PixelPickerActivity;
|
||||
import cc.winboll.studio.powerbell.model.BackgroundBean;
|
||||
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
||||
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -193,10 +193,10 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi
|
||||
public void onClick(View v) {
|
||||
dialog.dismiss();
|
||||
// 可以在这里添加确定后的回调逻辑
|
||||
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(PixelPickerActivity.this);
|
||||
BackgroundBean bean = utils.getCurrentBackgroundBean();
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this);
|
||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||
bean.setPixelColor(pixelColor);
|
||||
utils.saveSettings();
|
||||
utils.saveData();
|
||||
Toast.makeText(PixelPickerActivity.this, "已记录像素值", Toast.LENGTH_SHORT).show();
|
||||
setBackgroundColor();
|
||||
}
|
||||
@@ -217,8 +217,8 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi
|
||||
|
||||
|
||||
void setBackgroundColor() {
|
||||
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(PixelPickerActivity.this);
|
||||
BackgroundBean bean = utils.getCurrentBackgroundBean();
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this);
|
||||
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
|
||||
int nPixelColor = bean.getPixelColor();
|
||||
RelativeLayout mainLayout = findViewById(R.id.activitypixelpickerRelativeLayout1);
|
||||
mainLayout.setBackgroundColor(nPixelColor);
|
||||
@@ -235,7 +235,7 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(this, BackgroundSettingsActivity.class);
|
||||
intent.setClass(this, BackgroundPictureActivity.class);
|
||||
startActivity(intent);
|
||||
//GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(getApplicationContext(), );
|
||||
return true;
|
||||
@@ -248,7 +248,7 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi
|
||||
public void onBackPressed() {
|
||||
super.onBackPressed();
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(this, BackgroundSettingsActivity.class);
|
||||
intent.setClass(this, BackgroundPictureActivity.class);
|
||||
startActivity(intent);
|
||||
//GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(getApplicationContext(), BackgroundPictureActivity.class);
|
||||
}
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package cc.winboll.studio.powerbell.activities;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.utils.PermissionUtils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/27 14:26
|
||||
* @Describe 应用设置窗口
|
||||
*/
|
||||
public class SettingsActivity extends WinBoLLActivity implements IWinBoLLActivity {
|
||||
|
||||
public static final String TAG = "SettingsActivity";
|
||||
|
||||
private Toolbar mToolbar;
|
||||
|
||||
@Override
|
||||
public Activity getActivity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_settings);
|
||||
|
||||
mToolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(mToolbar);
|
||||
mToolbar.setSubtitle(getTag());
|
||||
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【导航栏】点击返回");
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onCheckPermission(View view) {
|
||||
//ToastUtils.show("onCheckPermission");
|
||||
if (PermissionUtils.getInstance().checkAndRequestStoragePermission(this)) {
|
||||
ToastUtils.show("【权限检查】存储权限已全部获取");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,6 @@ import android.widget.TextView;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
|
||||
import cc.winboll.studio.libaes.models.AESThemeBean;
|
||||
import cc.winboll.studio.libaes.utils.AESThemeUtil;
|
||||
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
|
||||
import cc.winboll.studio.powerbell.BuildConfig;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
|
||||
@@ -31,24 +28,15 @@ import cc.winboll.studio.powerbell.R;
|
||||
public abstract class WinBoLLActivity extends AppCompatActivity implements IWinBoLLActivity {
|
||||
|
||||
public static final String TAG = "WinBoLLActivity";
|
||||
|
||||
protected volatile AESThemeBean.ThemeType mThemeType;
|
||||
|
||||
protected TextView mTagView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
mThemeType = getThemeType();
|
||||
setThemeStyle();
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
AESThemeBean.ThemeType getThemeType() {
|
||||
return AESThemeBean.getThemeStyleType(AESThemeUtil.getThemeTypeID(getApplicationContext()));
|
||||
changeFullScreen(this);
|
||||
}
|
||||
|
||||
void setThemeStyle() {
|
||||
setTheme(AESThemeUtil.getThemeTypeID(getApplicationContext()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
@@ -91,13 +79,13 @@ public abstract class WinBoLLActivity extends AppCompatActivity implements IWinB
|
||||
@Override
|
||||
protected void onPostCreate(Bundle savedInstanceState) {
|
||||
super.onPostCreate(savedInstanceState);
|
||||
WinBoLLActivityManager.getInstance().add(this);
|
||||
//GlobalApplication.getWinBoLLActivityManager().add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
WinBoLLActivityManager.getInstance().registeRemove(this);
|
||||
//GlobalApplication.getWinBoLLActivityManager().registeRemove(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,7 +12,7 @@ import android.widget.TextView;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.adapters.BatteryAdapter;
|
||||
import cc.winboll.studio.powerbell.model.BatteryData;
|
||||
import cc.winboll.studio.powerbell.beans.BatteryData;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package cc.winboll.studio.powerbell.model;
|
||||
package cc.winboll.studio.powerbell.beans;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
@@ -0,0 +1,99 @@
|
||||
package cc.winboll.studio.powerbell.beans;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2024/07/18 11:52:28
|
||||
* @Describe 应用背景图片数据类
|
||||
*/
|
||||
import android.util.JsonReader;
|
||||
import android.util.JsonWriter;
|
||||
import cc.winboll.studio.libappbase.BaseBean;
|
||||
import java.io.IOException;
|
||||
|
||||
public class BackgroundPictureBean extends BaseBean {
|
||||
|
||||
public static final String TAG = "BackgroundPictureBean";
|
||||
|
||||
int backgroundWidth = 100;
|
||||
int backgroundHeight = 100;
|
||||
boolean isUseBackgroundFile = false;
|
||||
// 图片拾取像素颜色
|
||||
int pixelColor = 0;
|
||||
|
||||
public BackgroundPictureBean() {
|
||||
}
|
||||
|
||||
public BackgroundPictureBean(String recivedFileName, boolean isUseBackgroundFile) {
|
||||
this.isUseBackgroundFile = isUseBackgroundFile;
|
||||
}
|
||||
|
||||
public void setPixelColor(int pixelColor) {
|
||||
this.pixelColor = pixelColor;
|
||||
}
|
||||
|
||||
public int getPixelColor() {
|
||||
return pixelColor;
|
||||
}
|
||||
|
||||
public void setBackgroundWidth(int backgroundWidth) {
|
||||
this.backgroundWidth = backgroundWidth;
|
||||
}
|
||||
|
||||
public int getBackgroundWidth() {
|
||||
return backgroundWidth;
|
||||
}
|
||||
|
||||
public void setBackgroundHeight(int backgroundHeight) {
|
||||
this.backgroundHeight = backgroundHeight;
|
||||
}
|
||||
|
||||
public int getBackgroundHeight() {
|
||||
return backgroundHeight;
|
||||
}
|
||||
|
||||
public void setIsUseBackgroundFile(boolean isUseBackgroundFile) {
|
||||
this.isUseBackgroundFile = isUseBackgroundFile;
|
||||
}
|
||||
|
||||
public boolean isUseBackgroundFile() {
|
||||
return isUseBackgroundFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return BackgroundPictureBean.class.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
|
||||
super.writeThisToJsonWriter(jsonWriter);
|
||||
BackgroundPictureBean bean = this;
|
||||
jsonWriter.name("backgroundWidth").value(bean.getBackgroundWidth());
|
||||
jsonWriter.name("backgroundHeight").value(bean.getBackgroundHeight());
|
||||
jsonWriter.name("isUseBackgroundFile").value(bean.isUseBackgroundFile());
|
||||
jsonWriter.name("pixelColor").value(bean.getPixelColor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
|
||||
BackgroundPictureBean bean = new BackgroundPictureBean();
|
||||
jsonReader.beginObject();
|
||||
while (jsonReader.hasNext()) {
|
||||
String name = jsonReader.nextName();
|
||||
if (name.equals("backgroundWidth")) {
|
||||
bean.setBackgroundWidth(jsonReader.nextInt());
|
||||
} else if (name.equals("backgroundHeight")) {
|
||||
bean.setBackgroundHeight(jsonReader.nextInt());
|
||||
} else if (name.equals("isUseBackgroundFile")) {
|
||||
bean.setIsUseBackgroundFile(jsonReader.nextBoolean());
|
||||
} else if (name.equals("pixelColor")) {
|
||||
bean.setPixelColor(jsonReader.nextInt());
|
||||
} else {
|
||||
jsonReader.skipValue();
|
||||
}
|
||||
}
|
||||
// 结束 JSON 对象
|
||||
jsonReader.endObject();
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package cc.winboll.studio.powerbell.model;
|
||||
package cc.winboll.studio.powerbell.beans;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
@@ -1,4 +1,4 @@
|
||||
package cc.winboll.studio.powerbell.model;
|
||||
package cc.winboll.studio.powerbell.beans;
|
||||
|
||||
import android.util.JsonReader;
|
||||
import android.util.JsonWriter;
|
||||
@@ -1,4 +1,4 @@
|
||||
package cc.winboll.studio.powerbell.model;
|
||||
package cc.winboll.studio.powerbell.beans;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
@@ -1,4 +1,4 @@
|
||||
package cc.winboll.studio.powerbell.model;
|
||||
package cc.winboll.studio.powerbell.beans;
|
||||
|
||||
// 应用消息结构
|
||||
//
|
||||
@@ -12,8 +12,8 @@ import android.widget.Toast;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.MainActivity;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.activities.BackgroundSettingsActivity;
|
||||
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
|
||||
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
|
||||
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;
|
||||
BackgroundSourceUtils mBackgroundPictureUtils;
|
||||
BackgroundPictureUtils mBackgroundPictureUtils;
|
||||
Button dialogbackgroundpicturepreviewButton1;
|
||||
Button dialogbackgroundpicturepreviewButton2;
|
||||
String mszPreReceivedFileName;
|
||||
@@ -40,7 +40,7 @@ public class BackgroundPicturePreviewDialog extends Dialog {
|
||||
initEnv();
|
||||
|
||||
mContext = context;
|
||||
mBackgroundPictureUtils = BackgroundSourceUtils.getInstance(mContext);
|
||||
mBackgroundPictureUtils = ((BackgroundPictureActivity)context).mBackgroundPictureUtils;
|
||||
|
||||
ImageView imageView = findViewById(R.id.dialogbackgroundpicturepreviewImageView1);
|
||||
copyAndViewRecivePicture(imageView);
|
||||
@@ -78,7 +78,7 @@ public class BackgroundPicturePreviewDialog extends Dialog {
|
||||
|
||||
void copyAndViewRecivePicture(ImageView imageView) {
|
||||
//AppConfigUtils appConfigUtils = AppConfigUtils.getInstance((GlobalApplication)mContext.getApplicationContext());
|
||||
BackgroundSettingsActivity activity = ((BackgroundSettingsActivity)mContext);
|
||||
BackgroundPictureActivity activity = ((BackgroundPictureActivity)mContext);
|
||||
|
||||
//取出文件uri
|
||||
Uri uri = activity.getIntent().getData();
|
||||
@@ -95,7 +95,7 @@ public class BackgroundPicturePreviewDialog extends Dialog {
|
||||
|
||||
File fSrcImage = new File(szSrcImage);
|
||||
//mszPreReceivedFileName = DateUtils.getDateNowString() + "-" + fSrcImage.getName();
|
||||
File mfPreReceivedPhoto = new File(BackgroundSourceUtils.getInstance(mContext).getBackgroundSourceDirPath(), mszPreReceivedFileName);
|
||||
File mfPreReceivedPhoto = new File(activity.mBackgroundPictureUtils.getBackgroundDir(), mszPreReceivedFileName);
|
||||
// 复制源图片到剪裁文件
|
||||
try {
|
||||
FileUtils.copyFileUsingFileChannels(fSrcImage, mfPreReceivedPhoto);
|
||||
|
||||
@@ -15,12 +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;
|
||||
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
@@ -42,17 +41,15 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
||||
private Button btnConfirm;
|
||||
private Button btnPreview;
|
||||
private EditText etURL;
|
||||
BackgroundView mBackgroundView;
|
||||
BackgroundView bvBackgroundPreview;
|
||||
Context mContext;
|
||||
// 主线程 Handler,用于接收子线程消息并更新 UI
|
||||
private Handler mUiHandler;
|
||||
String mPreviewFilePath;
|
||||
String mPreviewFileUrl;
|
||||
String mDownloadSavedPath;
|
||||
String previewFilePath;
|
||||
|
||||
// 按钮点击回调接口(Java7 接口实现)
|
||||
public interface OnDialogClickListener {
|
||||
void onConfirm(String szConfirmFilePath, String previewFileUrl); // 确认按钮点击
|
||||
void onConfirm(); // 确认按钮点击
|
||||
void onCancel(); // 取消按钮点击
|
||||
}
|
||||
|
||||
@@ -90,12 +87,12 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
||||
switch (msg.what) {
|
||||
case MSG_IMAGE_LOAD_SUCCESS:
|
||||
// 图片加载成功,获取文件路径并设置背景
|
||||
mDownloadSavedPath = (String) msg.obj;
|
||||
previewBackground(mDownloadSavedPath);
|
||||
String filePath = (String) msg.obj;
|
||||
setBackgroundFromPath(filePath);
|
||||
break;
|
||||
case MSG_IMAGE_LOAD_FAILED:
|
||||
// 图片加载失败,设置默认背景
|
||||
mBackgroundView.setBackgroundResource(R.drawable.ic_launcher);
|
||||
bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
|
||||
ToastUtils.show("图片预览失败,请检查链接");
|
||||
break;
|
||||
}
|
||||
@@ -137,9 +134,8 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
||||
btnConfirm = (Button) dialogView.findViewById(R.id.btn_confirm);
|
||||
btnPreview = (Button) dialogView.findViewById(R.id.btn_preview);
|
||||
etURL = (EditText) dialogView.findViewById(R.id.et_url);
|
||||
mBackgroundView = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview);
|
||||
// 加载初始图片
|
||||
mBackgroundView.setBackgroundResource(R.drawable.ic_launcher);
|
||||
bvBackgroundPreview = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview);
|
||||
|
||||
// 设置按钮点击事件
|
||||
setButtonClickListeners();
|
||||
}
|
||||
@@ -153,9 +149,6 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d("NetworkBackgroundDialog", "取消按钮点击");
|
||||
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(mContext);
|
||||
utils.setCurrentSourceToPreview();
|
||||
|
||||
dismiss(); // 关闭对话框
|
||||
if (listener != null) {
|
||||
listener.onCancel();
|
||||
@@ -169,12 +162,11 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
||||
public void onClick(View v) {
|
||||
LogUtils.d("NetworkBackgroundDialog", "确认按钮点击");
|
||||
// 确定预览背景资源
|
||||
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(mContext);
|
||||
utils.saveFileToPreviewBean(new File(mPreviewFilePath), mPreviewFileUrl);
|
||||
bvBackgroundPreview.saveToBackgroundSources(previewFilePath);
|
||||
|
||||
dismiss(); // 关闭对话框
|
||||
if (listener != null) {
|
||||
listener.onConfirm(mPreviewFilePath, mPreviewFileUrl);
|
||||
listener.onConfirm();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -183,7 +175,14 @@ 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);*/
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -192,25 +191,26 @@ public class NetworkBackgroundDialog extends AlertDialog {
|
||||
* 根据文件路径设置 BackgroundView 背景(主线程调用)
|
||||
* @param filePath 图片文件路径
|
||||
*/
|
||||
private void previewBackground(String previewFilePath) {
|
||||
private void setBackgroundFromPath(String filePath) {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
File imageFile = new File(previewFilePath);
|
||||
File imageFile = new File(filePath);
|
||||
if (!imageFile.exists()) {
|
||||
ToastUtils.show("图片文件不存在:" + previewFilePath);
|
||||
LogUtils.e(TAG, "图片文件不存在:" + previewFilePath);
|
||||
mBackgroundView.setBackgroundResource(R.drawable.ic_launcher);
|
||||
LogUtils.e(TAG, "图片文件不存在:" + filePath);
|
||||
ToastUtils.show("Test");
|
||||
//bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
|
||||
return;
|
||||
}
|
||||
|
||||
// 预览背景
|
||||
mPreviewFilePath = previewFilePath;
|
||||
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(mContext);
|
||||
utils.saveFileToPreviewBean(new File(mPreviewFilePath), mPreviewFileUrl);
|
||||
mBackgroundView.loadBackgroundBean(utils.getPreviewBackgroundBean());
|
||||
previewFilePath = filePath;
|
||||
bvBackgroundPreview.previewBackgroundImage(previewFilePath);
|
||||
|
||||
LogUtils.d(TAG, "图片预览成功:" + filePath);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
mBackgroundView.setBackgroundResource(R.drawable.ic_launcher);
|
||||
bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
|
||||
LogUtils.e(TAG, "图片预览失败:" + e.getMessage());
|
||||
} finally {
|
||||
// Java7 手动关闭流,避免资源泄漏
|
||||
@@ -249,20 +249,40 @@ 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 previewFileUrl = "https://example.com/test.jpg";
|
||||
mPreviewFileUrl = etURL.getText().toString();
|
||||
ImageDownloader.getInstance(mContext).downloadImage(mPreviewFileUrl, new ImageDownloader.DownloadCallback(){
|
||||
//String imgUrl = "https://example.com/test.jpg";
|
||||
String imgUrl = etURL.getText().toString();
|
||||
PictureUtils.downloadImageToAlbum(mContext, imgUrl, new PictureUtils.DownloadCallback(){
|
||||
@Override
|
||||
public void onSuccess(String savePath) {
|
||||
ToastUtils.show("下载成功:" + savePath);
|
||||
// 发送消息到主线程,携带图片路径
|
||||
Message successMsg = mUiHandler.obtainMessage(MSG_IMAGE_LOAD_SUCCESS, savePath);
|
||||
mUiHandler.sendMessage(successMsg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(String errorMsg) {
|
||||
ToastUtils.show("下载失败:" + errorMsg);
|
||||
public void onFailure(Exception e) {
|
||||
ToastUtils.show("下载失败:" + e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -19,11 +19,8 @@ import android.widget.TextView;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.App;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.activities.PixelPickerActivity;
|
||||
import cc.winboll.studio.powerbell.model.BackgroundBean;
|
||||
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
||||
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
|
||||
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;
|
||||
@@ -33,8 +30,328 @@ public class MainViewFragment extends Fragment {
|
||||
|
||||
public static final String TAG = "MainViewFragment";
|
||||
|
||||
|
||||
|
||||
public static final int MSG_RELOAD_APPCONFIG = 0;
|
||||
public static final int MSG_CURRENTVALUEBATTERY = 1;
|
||||
|
||||
static MainViewFragment _mMainViewFragment;
|
||||
AppConfigUtils mAppConfigUtils;
|
||||
View mView;
|
||||
Drawable mDrawableFrame;
|
||||
LinearLayout mllLeftSeekBar;
|
||||
LinearLayout mllRightSeekBar;
|
||||
CheckBox mcbIsEnableChargeReminder;
|
||||
CheckBox mcbIsEnableUsegeReminder;
|
||||
Switch mswIsEnableService;
|
||||
TextView mtvTips;
|
||||
|
||||
// 背景布局
|
||||
//LinearLayout mLinearLayoutloadBackground;
|
||||
|
||||
// 现在电量图示
|
||||
BatteryDrawable mCurrentValueBatteryDrawable;
|
||||
// 现在充电提醒电量图示
|
||||
BatteryDrawable mChargeReminderValueBatteryDrawable;
|
||||
// 现在耗电提醒电量图示
|
||||
BatteryDrawable mUsegeReminderValueBatteryDrawable;
|
||||
|
||||
ImageView mCurrentValueBatteryImageView;
|
||||
ImageView mChargeReminderValueBatteryImageView;
|
||||
ImageView mUsegeReminderValueBatteryImageView;
|
||||
|
||||
VerticalSeekBar mChargeReminderSeekBar;
|
||||
ChargeReminderSeekBarChangeListener mChargeReminderSeekBarChangeListener;
|
||||
TextView mtvChargeReminderValue;
|
||||
|
||||
|
||||
VerticalSeekBar mUsegeReminderSeekBar;
|
||||
UsegeReminderSeekBarChangeListener mUsegeReminderSeekBarChangeListener;
|
||||
TextView mtvUsegeReminderValue;
|
||||
CheckBox mcbUsegeReminderValue;
|
||||
TextView mtvCurrentValue;
|
||||
BackgroundView bvPreviewBackground;
|
||||
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
mView = inflater.inflate(R.layout.fragment_mainview, container, false);
|
||||
_mMainViewFragment = MainViewFragment.this;
|
||||
mAppConfigUtils = App.getAppConfigUtils(getActivity());
|
||||
|
||||
// 获取指定ID的View实例
|
||||
bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1);
|
||||
/*final View mainImageView = mView.findViewById(R.id.fragmentmainviewImageView1);
|
||||
|
||||
// 注册OnGlobalLayoutListener
|
||||
mainImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
// 获取宽度和高度
|
||||
int width = mainImageView.getMeasuredWidth();
|
||||
int height = mainImageView.getMeasuredHeight();
|
||||
|
||||
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity());
|
||||
BackgroundPictureBean bean = utils.loadBackgroundPictureBean();
|
||||
bean.setBackgroundWidth(width);
|
||||
bean.setBackgroundHeight(height);
|
||||
utils.saveData();
|
||||
// 移除监听器以避免内存泄漏
|
||||
mainImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
||||
}
|
||||
});*/
|
||||
|
||||
mDrawableFrame = getActivity().getDrawable(R.drawable.bg_frame);
|
||||
mllLeftSeekBar = (LinearLayout) mView.findViewById(R.id.fragmentmainviewLinearLayout1);
|
||||
mllRightSeekBar = (LinearLayout) mView.findViewById(R.id.fragmentmainviewLinearLayout2);
|
||||
|
||||
// 初始化充电电量提醒设置控件
|
||||
mtvChargeReminderValue = (TextView) mView.findViewById(R.id.fragmentandroidviewTextView2);
|
||||
mChargeReminderSeekBar = (VerticalSeekBar) mView.findViewById(R.id.fragmentandroidviewVerticalSeekBar1);
|
||||
mcbIsEnableChargeReminder = mView.findViewById(R.id.fragmentmainviewCheckBox1);
|
||||
|
||||
// 初始化耗电电量提醒设置控件
|
||||
mtvUsegeReminderValue = (TextView) mView.findViewById(R.id.fragmentandroidviewTextView3);
|
||||
mUsegeReminderSeekBar = (VerticalSeekBar) mView.findViewById(R.id.fragmentandroidviewVerticalSeekBar2);
|
||||
mcbIsEnableUsegeReminder = mView.findViewById(R.id.fragmentmainviewCheckBox2);
|
||||
|
||||
// 初始化现在电量显示控件
|
||||
mtvCurrentValue = (TextView) mView.findViewById(R.id.fragmentandroidviewTextView4);
|
||||
|
||||
// 初始化服务总开关
|
||||
mswIsEnableService = (Switch) mView.findViewById(R.id.fragmentandroidviewSwitch1);
|
||||
mtvTips = mView.findViewById(R.id.fragmentandroidviewTextView1);
|
||||
|
||||
// 设置视图显示数据
|
||||
setViewData();
|
||||
// 设置视图控件响应
|
||||
setViewListener();
|
||||
|
||||
// 注册一个广播接收
|
||||
//mMainActivityReceiver = new MainActivityReceiver(this);
|
||||
//mMainActivityReceiver.registerAction();
|
||||
|
||||
// 启动的时候检查一下服务
|
||||
if (mAppConfigUtils.getIsEnableService()
|
||||
&& ServiceUtils.isServiceAlive(getActivity(), ControlCenterService.class.getName()) == false) {
|
||||
// 如果配置了服务启动,服务没有启动
|
||||
// 就启动服务
|
||||
Intent intent = new Intent(getActivity(), ControlCenterService.class);
|
||||
getActivity().startForegroundService(intent);
|
||||
}
|
||||
|
||||
return mView;
|
||||
}
|
||||
|
||||
void setViewData() {
|
||||
int nChargeReminderValue = mAppConfigUtils.getChargeReminderValue();
|
||||
int nUsegeReminderValue = mAppConfigUtils.getUsegeReminderValue();
|
||||
int nCurrentValue = mAppConfigUtils.getCurrentValue();
|
||||
|
||||
mllLeftSeekBar.setBackground(mDrawableFrame);
|
||||
mllRightSeekBar.setBackground(mDrawableFrame);
|
||||
|
||||
// 初始化电量图
|
||||
mCurrentValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorCurrent));
|
||||
mCurrentValueBatteryDrawable.setValue(mAppConfigUtils.getCurrentValue());
|
||||
mCurrentValueBatteryImageView = mView.findViewById(R.id.fragmentandroidviewImageView1);
|
||||
mCurrentValueBatteryImageView.setImageDrawable(mCurrentValueBatteryDrawable);
|
||||
|
||||
// 初始化充电电量提醒图
|
||||
mChargeReminderValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorCharge));
|
||||
mChargeReminderValueBatteryDrawable.setValue(nChargeReminderValue);
|
||||
mChargeReminderValueBatteryImageView = mView.findViewById(R.id.fragmentandroidviewImageView3);
|
||||
mChargeReminderValueBatteryImageView.setImageDrawable(mChargeReminderValueBatteryDrawable);
|
||||
|
||||
// 初始化耗电电量提醒图
|
||||
mUsegeReminderValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorUsege));
|
||||
mUsegeReminderValueBatteryDrawable.setValue(nUsegeReminderValue);
|
||||
mUsegeReminderValueBatteryImageView = mView.findViewById(R.id.fragmentandroidviewImageView2);
|
||||
mUsegeReminderValueBatteryImageView.setImageDrawable(mUsegeReminderValueBatteryDrawable);
|
||||
|
||||
// 初始化充电电量提醒设置控件
|
||||
mtvChargeReminderValue.setTextColor(getActivity().getColor(R.color.colorCharge));
|
||||
//LogUtils.d(TAG, "Color.YELLOW is " + Integer.toString(mApplication.getColor(R.color.colorCharge)));
|
||||
mtvChargeReminderValue.setText(Integer.toString(nChargeReminderValue) + "%");
|
||||
mChargeReminderSeekBar.setProgress(nChargeReminderValue);
|
||||
mcbIsEnableChargeReminder.setChecked(mAppConfigUtils.getIsEnableChargeReminder());
|
||||
|
||||
// 初始化耗电电量提醒设置控件
|
||||
mtvUsegeReminderValue.setTextColor(getActivity().getColor(R.color.colorUsege));
|
||||
mtvUsegeReminderValue.setText(Integer.toString(nUsegeReminderValue) + "%");
|
||||
mUsegeReminderSeekBar.setProgress(nUsegeReminderValue);
|
||||
mcbIsEnableUsegeReminder.setChecked(mAppConfigUtils.getIsEnableUsegeReminder());
|
||||
|
||||
// 初始化现在电量显示控件
|
||||
mtvCurrentValue.setTextColor(getActivity().getColor(R.color.colorCurrent));
|
||||
mtvCurrentValue.setText(Integer.toString(nCurrentValue) + "%");
|
||||
|
||||
// 初始化服务总开关
|
||||
mswIsEnableService.setChecked(mAppConfigUtils.getIsEnableService());
|
||||
if (mAppConfigUtils.getIsEnableService()) {
|
||||
//LogUtils.d(TAG, "mApplication.getIsEnableService() " + Boolean.toString(mAppConfigUtils.getIsEnableService()));
|
||||
ControlCenterService.startControlCenterService(getActivity());
|
||||
} else {
|
||||
//LogUtils.d(TAG, "mApplication.getIsEnableService() " + Boolean.toString(mAppConfigUtils.getIsEnableService()));
|
||||
ControlCenterService.stopControlCenterService(getActivity());
|
||||
}
|
||||
mswIsEnableService.setText(getString(R.string.txt_aboveswitch));
|
||||
mtvTips.setText(getString(R.string.txt_aboveswitchtips));
|
||||
|
||||
}
|
||||
|
||||
void setViewListener() {
|
||||
// 初始化充电电量提醒设置控件
|
||||
mChargeReminderSeekBarChangeListener = new ChargeReminderSeekBarChangeListener();
|
||||
mChargeReminderSeekBar.setOnSeekBarChangeListener(mChargeReminderSeekBarChangeListener);
|
||||
mcbIsEnableChargeReminder.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "setIsEnableChargeReminder");
|
||||
mAppConfigUtils.setIsEnableChargeReminder(mcbIsEnableChargeReminder.isChecked());
|
||||
//ControlCenterService.updateIsEnableChargeReminder(mcbIsEnableChargeReminder.isChecked());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 初始化耗电电量提醒设置控件
|
||||
mUsegeReminderSeekBarChangeListener = new UsegeReminderSeekBarChangeListener();
|
||||
mUsegeReminderSeekBar.setOnSeekBarChangeListener(mUsegeReminderSeekBarChangeListener);
|
||||
mcbIsEnableUsegeReminder.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "setIsEnableUsegeReminder");
|
||||
mAppConfigUtils.setIsEnableUsegeReminder(mcbIsEnableUsegeReminder.isChecked());
|
||||
//ControlCenterService.updateIsEnableUsegeReminder(mcbIsEnableUsegeReminder.isChecked());
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化服务总开关
|
||||
mswIsEnableService.setOnClickListener(new CompoundButton.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
mAppConfigUtils.setIsEnableService(getActivity(), mswIsEnableService.isChecked());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void setCurrentValueBattery(int value) {
|
||||
//LogUtils.d(TAG, "setCurrentValueBattery");
|
||||
mtvCurrentValue.setText(Integer.toString(value) + "%");
|
||||
mCurrentValueBatteryDrawable.setValue(value);
|
||||
mCurrentValueBatteryDrawable.invalidateSelf();
|
||||
}
|
||||
|
||||
class ChargeReminderSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
|
||||
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
//LogUtils.d(TAG, "call onProgressChanged");
|
||||
int nChargeReminderValue = progress;
|
||||
mtvChargeReminderValue.setText(Integer.toString(nChargeReminderValue) + "%");
|
||||
mChargeReminderValueBatteryDrawable.setValue(nChargeReminderValue);
|
||||
mChargeReminderValueBatteryDrawable.invalidateSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
//LogUtils.d(TAG, "call onStartTrackingTouch");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
//LogUtils.d(TAG, "call onStopTrackingTouch");
|
||||
//取得当前进度条的刻度
|
||||
int nChargeReminderValue = ((VerticalSeekBar)seekBar)._mnProgress;
|
||||
|
||||
mAppConfigUtils.setChargeReminderValue(nChargeReminderValue);
|
||||
mtvChargeReminderValue.setText(Integer.toString(nChargeReminderValue) + "%");
|
||||
//ControlCenterService.updateChargeReminderValue(nChargeReminderValue);
|
||||
}
|
||||
}
|
||||
|
||||
class UsegeReminderSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
|
||||
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
//LogUtils.d(TAG, "call onProgressChanged");
|
||||
int nUsegeReminderValue = progress;
|
||||
mtvUsegeReminderValue.setText(Integer.toString(nUsegeReminderValue) + "%");
|
||||
mUsegeReminderValueBatteryDrawable.setValue(nUsegeReminderValue);
|
||||
mUsegeReminderValueBatteryDrawable.invalidateSelf();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
//LogUtils.d(TAG, "call onStartTrackingTouch");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
//LogUtils.d(TAG, "call onStopTrackingTouch");
|
||||
//取得当前进度条的刻度
|
||||
int nUsegeReminderValue = ((VerticalSeekBar)seekBar)._mnProgress;
|
||||
LogUtils.d(TAG, "nUsegeReminderValue is " + Integer.toString(nUsegeReminderValue));
|
||||
//LogUtils.d(TAG, "mPowerReminder is " + mApplication);
|
||||
mAppConfigUtils.setUsegeReminderValue(nUsegeReminderValue);
|
||||
//LogUtils.d(TAG, "opopopopopopopop");
|
||||
mtvUsegeReminderValue.setText(Integer.toString(nUsegeReminderValue) + "%");
|
||||
//ControlCenterService.updateUsegeReminderValue(nUsegeReminderValue);
|
||||
}
|
||||
}
|
||||
|
||||
public void reloadBackground() {
|
||||
bvPreviewBackground.reloadBackgroundImage();
|
||||
// BackgroundPictureBean bean = BackgroundPictureUtils.getInstance(getActivity()).getBackgroundPictureBean();
|
||||
// ImageView imageView = mView.findViewById(R.id.fragmentmainviewImageView1);
|
||||
// String szBackgroundFilePath = BackgroundPictureUtils.getInstance(getActivity()).getBackgroundDir() + BackgroundPictureActivity.getBackgroundFileName();
|
||||
// File fBackgroundFilePath = new File(szBackgroundFilePath);
|
||||
// LogUtils.d(TAG, "szBackgroundFilePath : " + szBackgroundFilePath);
|
||||
// LogUtils.d(TAG, String.format("fBackgroundFilePath.exists() %s", fBackgroundFilePath.exists()));
|
||||
// if (bean.isUseBackgroundFile() && fBackgroundFilePath.exists()) {
|
||||
// Drawable drawableBackground = Drawable.createFromPath(szBackgroundFilePath);
|
||||
// //drawableBackground.setAlpha(120);
|
||||
// imageView.setImageDrawable(drawableBackground);
|
||||
// } else {
|
||||
// Drawable drawableBackground = getActivity().getDrawable(R.drawable.blank10x10);
|
||||
// //drawableBackground.setAlpha(120);
|
||||
// imageView.setImageDrawable(drawableBackground);
|
||||
// }
|
||||
}
|
||||
|
||||
Handler mHandler = new Handler(){
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
switch (msg.what) {
|
||||
case MSG_RELOAD_APPCONFIG : {
|
||||
setViewData();
|
||||
break;
|
||||
}
|
||||
case MSG_CURRENTVALUEBATTERY : {
|
||||
setCurrentValueBattery(msg.arg1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
super.handleMessage(msg);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static void relaodAppConfigs() {
|
||||
if (_mMainViewFragment != null) {
|
||||
Handler handler = _mMainViewFragment.mHandler;
|
||||
handler.sendMessage(handler.obtainMessage(MSG_RELOAD_APPCONFIG));
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendMsgCurrentValueBattery(int value) {
|
||||
if (_mMainViewFragment != null) {
|
||||
Handler handler = _mMainViewFragment.mHandler;
|
||||
Message msg = handler.obtainMessage(MSG_CURRENTVALUEBATTERY);
|
||||
msg.arg1 = value;
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,254 +0,0 @@
|
||||
package cc.winboll.studio.powerbell.model;
|
||||
|
||||
import android.util.JsonReader;
|
||||
import android.util.JsonWriter;
|
||||
import cc.winboll.studio.libappbase.BaseBean;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2024/07/18 11:52:28
|
||||
* @Describe 应用背景图片数据类(存储正式/预览背景配置,支持JSON序列化/反序列化)
|
||||
*/
|
||||
public class BackgroundBean extends BaseBean {
|
||||
|
||||
public static final String TAG = "BackgroundPictureBean";
|
||||
|
||||
// 核心字段:背景图片文件名(对应应用私有目录下的图片文件,与BackgroundSettingsActivity的_mSourceCroppedFile匹配)
|
||||
private String backgroundFileName = "";
|
||||
// 核心字段:背景图片完整路径(解决仅存文件名导致的路径拼接错误,与backgroundScaledCompressFilePath对应)
|
||||
private String backgroundFilePath = "";
|
||||
// 附加字段:图片信息(如Uri、网络地址等,仅作备注,不参与路径生成)
|
||||
private String backgroundFileInfo = "";
|
||||
// 控制字段:是否启用背景图片(true-显示背景图,false-显示透明背景)
|
||||
private boolean isUseBackgroundFile = false;
|
||||
// 核心字段:压缩后背景图片文件名(对应应用私有目录下的压缩图片,与saveCropBitmap的压缩图匹配)
|
||||
private String backgroundScaledCompressFileName = "";
|
||||
// 核心字段:压缩后背景图片完整路径(解决仅存文件名导致的路径拼接错误,适配BackgroundSettingsActivity的私有目录)
|
||||
private String backgroundScaledCompressFilePath = "";
|
||||
// 重命名字段:是否启用压缩背景图(原isUseScaledCompress → 新isUseBackgroundScaledCompressFile,语义更清晰)
|
||||
private boolean isUseBackgroundScaledCompressFile = false;
|
||||
// 裁剪比例字段:背景图宽高比(默认1:1,用于固定比例裁剪)
|
||||
private int backgroundWidth = 100;
|
||||
private int backgroundHeight = 100;
|
||||
// 像素拾取字段:拾取的像素颜色(用于纯色背景)
|
||||
private int pixelColor = 0;
|
||||
|
||||
/**
|
||||
* 无参构造器(必须,JSON反序列化时需默认构造器)
|
||||
*/
|
||||
public BackgroundBean() {
|
||||
}
|
||||
|
||||
// ====================================== Getter/Setter 方法(全字段,含重命名+新增字段)======================================
|
||||
public String getBackgroundFileName() {
|
||||
return backgroundFileName;
|
||||
}
|
||||
|
||||
public void setBackgroundFileName(String backgroundFileName) {
|
||||
this.backgroundFileName = backgroundFileName == null ? "" : backgroundFileName; // 防null,避免空指针
|
||||
}
|
||||
|
||||
public String getBackgroundFilePath() {
|
||||
return backgroundFilePath;
|
||||
}
|
||||
|
||||
public void setBackgroundFilePath(String backgroundFilePath) {
|
||||
this.backgroundFilePath = backgroundFilePath == null ? "" : backgroundFilePath; // 防null,避免路径拼接错误
|
||||
}
|
||||
|
||||
public String getBackgroundFileInfo() {
|
||||
return backgroundFileInfo;
|
||||
}
|
||||
|
||||
public void setBackgroundFileInfo(String backgroundFileInfo) {
|
||||
this.backgroundFileInfo = backgroundFileInfo == null ? "" : backgroundFileInfo; // 防null,避免空指针
|
||||
}
|
||||
|
||||
public boolean isUseBackgroundFile() {
|
||||
return isUseBackgroundFile;
|
||||
}
|
||||
|
||||
public void setIsUseBackgroundFile(boolean isUseBackgroundFile) {
|
||||
this.isUseBackgroundFile = isUseBackgroundFile;
|
||||
}
|
||||
|
||||
public String getBackgroundScaledCompressFileName() {
|
||||
return backgroundScaledCompressFileName;
|
||||
}
|
||||
|
||||
public void setBackgroundScaledCompressFileName(String backgroundScaledCompressFileName) {
|
||||
this.backgroundScaledCompressFileName = backgroundScaledCompressFileName == null ? "" : backgroundScaledCompressFileName; // 防null
|
||||
}
|
||||
|
||||
public String getBackgroundScaledCompressFilePath() {
|
||||
return backgroundScaledCompressFilePath;
|
||||
}
|
||||
|
||||
public void setBackgroundScaledCompressFilePath(String backgroundScaledCompressFilePath) {
|
||||
this.backgroundScaledCompressFilePath = backgroundScaledCompressFilePath == null ? "" : backgroundScaledCompressFilePath; // 防null,避免路径错误
|
||||
}
|
||||
|
||||
/**
|
||||
* 重命名:原isUseScaledCompress → 新isUseBackgroundScaledCompressFile(Getter/Setter同步修改)
|
||||
* 语义:明确表示“是否启用背景压缩图文件”,避免与其他压缩逻辑混淆
|
||||
*/
|
||||
public boolean isUseBackgroundScaledCompressFile() {
|
||||
return isUseBackgroundScaledCompressFile;
|
||||
}
|
||||
|
||||
public void setIsUseBackgroundScaledCompressFile(boolean isUseBackgroundScaledCompressFile) {
|
||||
this.isUseBackgroundScaledCompressFile = isUseBackgroundScaledCompressFile;
|
||||
}
|
||||
|
||||
public int getBackgroundWidth() {
|
||||
return backgroundWidth;
|
||||
}
|
||||
|
||||
public void setBackgroundWidth(int backgroundWidth) {
|
||||
this.backgroundWidth = backgroundWidth <= 0 ? 100 : backgroundWidth; // 防无效值,确保宽高比有效
|
||||
}
|
||||
|
||||
public int getBackgroundHeight() {
|
||||
return backgroundHeight;
|
||||
}
|
||||
|
||||
public void setBackgroundHeight(int backgroundHeight) {
|
||||
this.backgroundHeight = backgroundHeight <= 0 ? 100 : backgroundHeight; // 防无效值,确保宽高比有效
|
||||
}
|
||||
|
||||
public int getPixelColor() {
|
||||
return pixelColor;
|
||||
}
|
||||
|
||||
public void setPixelColor(int pixelColor) {
|
||||
this.pixelColor = pixelColor;
|
||||
}
|
||||
|
||||
// ====================================== 序列化/反序列化方法(适配重命名字段,兼容旧版本)======================================
|
||||
@Override
|
||||
public String getName() {
|
||||
return BackgroundBean.class.getName(); // 必须重写,BaseBean序列化时需类名标识
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化:同步重命名字段(原isUseScaledCompress → 新isUseBackgroundScaledCompressFile)
|
||||
* 确保新字段能正常持久化,同时兼容旧版本JSON(可选:保留旧字段写入,避免旧版本读取异常)
|
||||
*/
|
||||
@Override
|
||||
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
|
||||
super.writeThisToJsonWriter(jsonWriter);
|
||||
BackgroundBean bean = this;
|
||||
jsonWriter.name("backgroundFileName").value(bean.getBackgroundFileName());
|
||||
jsonWriter.name("backgroundFilePath").value(bean.getBackgroundFilePath()); // 新增字段:背景原图完整路径
|
||||
jsonWriter.name("backgroundFileInfo").value(bean.getBackgroundFileInfo());
|
||||
jsonWriter.name("isUseBackgroundFile").value(bean.isUseBackgroundFile());
|
||||
jsonWriter.name("backgroundScaledCompressFileName").value(bean.getBackgroundScaledCompressFileName());
|
||||
jsonWriter.name("backgroundScaledCompressFilePath").value(bean.getBackgroundScaledCompressFilePath());
|
||||
// 关键:新字段序列化(核心)
|
||||
jsonWriter.name("isUseBackgroundScaledCompressFile").value(bean.isUseBackgroundScaledCompressFile());
|
||||
// 兼容旧版本:保留旧字段名写入(可选,避免旧版本Bean读取时缺失字段)
|
||||
jsonWriter.name("isUseScaledCompress").value(bean.isUseBackgroundScaledCompressFile());
|
||||
jsonWriter.name("backgroundWidth").value(bean.getBackgroundWidth());
|
||||
jsonWriter.name("backgroundHeight").value(bean.getBackgroundHeight());
|
||||
jsonWriter.name("pixelColor").value(bean.getPixelColor());
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化:同步处理重命名字段(兼容旧版本JSON,新旧字段都能读取)
|
||||
* 逻辑:优先读取新字段,若新字段不存在则读取旧字段(确保升级后旧配置仍有效)
|
||||
*/
|
||||
@Override
|
||||
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
|
||||
BackgroundBean bean = new BackgroundBean();
|
||||
jsonReader.beginObject();
|
||||
// 临时变量:存储旧字段值(用于兼容)
|
||||
boolean tempUseScaledCompress = false;
|
||||
while (jsonReader.hasNext()) {
|
||||
String name = jsonReader.nextName();
|
||||
switch (name) {
|
||||
case "backgroundFileName":
|
||||
bean.setBackgroundFileName(jsonReader.nextString());
|
||||
break;
|
||||
case "backgroundFilePath":
|
||||
bean.setBackgroundFilePath(jsonReader.nextString()); // 新增字段:读取背景原图完整路径
|
||||
break;
|
||||
case "backgroundFileInfo":
|
||||
bean.setBackgroundFileInfo(jsonReader.nextString());
|
||||
break;
|
||||
case "isUseBackgroundFile":
|
||||
bean.setIsUseBackgroundFile(jsonReader.nextBoolean());
|
||||
break;
|
||||
case "backgroundScaledCompressFileName":
|
||||
bean.setBackgroundScaledCompressFileName(jsonReader.nextString());
|
||||
break;
|
||||
case "backgroundScaledCompressFilePath":
|
||||
bean.setBackgroundScaledCompressFilePath(jsonReader.nextString());
|
||||
break;
|
||||
// 关键:读取新字段(优先)
|
||||
case "isUseBackgroundScaledCompressFile":
|
||||
bean.setIsUseBackgroundScaledCompressFile(jsonReader.nextBoolean());
|
||||
break;
|
||||
// 兼容旧版本:读取旧字段(若新字段未读取,则用旧字段值)
|
||||
case "isUseScaledCompress":
|
||||
tempUseScaledCompress = jsonReader.nextBoolean();
|
||||
break;
|
||||
case "backgroundWidth":
|
||||
bean.setBackgroundWidth(jsonReader.nextInt());
|
||||
break;
|
||||
case "backgroundHeight":
|
||||
bean.setBackgroundHeight(jsonReader.nextInt());
|
||||
break;
|
||||
case "pixelColor":
|
||||
bean.setPixelColor(jsonReader.nextInt());
|
||||
break;
|
||||
default:
|
||||
jsonReader.skipValue(); // 跳过未知字段,兼容旧版本Bean(避免崩溃)
|
||||
break;
|
||||
}
|
||||
}
|
||||
jsonReader.endObject();
|
||||
// 兼容逻辑:若新字段未被赋值(旧版本JSON无此字段),则用旧字段值填充
|
||||
if (!jsonReader.toString().contains("isUseBackgroundScaledCompressFile")) {
|
||||
bean.setIsUseBackgroundScaledCompressFile(tempUseScaledCompress);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
// ====================================== 辅助方法(同步更新重命名字段)======================================
|
||||
/**
|
||||
* 重置背景配置(适配“取消背景”功能,同步重置重命名字段)
|
||||
*/
|
||||
public void resetBackgroundConfig() {
|
||||
this.backgroundFileName = "";
|
||||
this.backgroundFilePath = ""; // 新增:重置背景原图完整路径
|
||||
this.backgroundScaledCompressFileName = "";
|
||||
this.backgroundScaledCompressFilePath = "";
|
||||
this.backgroundFileInfo = "";
|
||||
this.isUseBackgroundFile = false;
|
||||
this.isUseBackgroundScaledCompressFile = false; // 重命名字段:重置为false
|
||||
this.backgroundWidth = 100;
|
||||
this.backgroundHeight = 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查背景配置是否有效(适配BackgroundSettingsActivity的预览/保存校验)
|
||||
* 同步使用重命名字段判断压缩图是否启用
|
||||
* @return true-配置有效(可显示背景图),false-配置无效
|
||||
*/
|
||||
public boolean isBackgroundConfigValid() {
|
||||
// 启用背景图时,需确保:原图路径/文件名 或 压缩图路径/文件名 非空
|
||||
if (!isUseBackgroundFile) {
|
||||
return false;
|
||||
}
|
||||
// 原图校验:路径非空 或 文件名非空
|
||||
boolean isOriginalValid = !backgroundFilePath.isEmpty() || !backgroundFileName.isEmpty();
|
||||
// 压缩图校验:启用压缩图时,路径/文件名需非空
|
||||
boolean isCompressValid = true;
|
||||
if (isUseBackgroundScaledCompressFile()) { // 重命名字段:判断是否启用压缩图
|
||||
isCompressValid = !backgroundScaledCompressFilePath.isEmpty() || !backgroundScaledCompressFileName.isEmpty();
|
||||
}
|
||||
// 逻辑:启用压缩图则需压缩图有效;不启用压缩图则需原图有效
|
||||
return isUseBackgroundScaledCompressFile() ? isCompressValid : isOriginalValid;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.model.AppConfigBean;
|
||||
import cc.winboll.studio.powerbell.beans.AppConfigBean;
|
||||
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
||||
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
|
||||
import cc.winboll.studio.powerbell.utils.BatteryUtils;
|
||||
|
||||
@@ -5,9 +5,10 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import cc.winboll.studio.powerbell.App;
|
||||
import cc.winboll.studio.powerbell.MainActivity;
|
||||
import cc.winboll.studio.powerbell.fragments.MainViewFragment;
|
||||
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
|
||||
import cc.winboll.studio.powerbell.utils.BatteryUtils;
|
||||
import cc.winboll.studio.powerbell.utils.NotificationHelper;
|
||||
|
||||
public class GlobalApplicationReceiver extends BroadcastReceiver {
|
||||
|
||||
@@ -47,7 +48,7 @@ public class GlobalApplicationReceiver extends BroadcastReceiver {
|
||||
//NotificationHelper.cancelRemindNotification(context);
|
||||
|
||||
App.getAppCacheUtils(context).addChangingTime(nTheQuantityOfElectricity);
|
||||
MainActivity.sendMsgCurrentValueBattery(nTheQuantityOfElectricity);
|
||||
MainViewFragment.sendMsgCurrentValueBattery(nTheQuantityOfElectricity);
|
||||
// 保存好新的电池状态标志
|
||||
_mIsCharging = isCharging;
|
||||
_mnTheQuantityOfElectricityOld = nTheQuantityOfElectricity;
|
||||
|
||||
@@ -23,8 +23,8 @@ import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.App;
|
||||
import cc.winboll.studio.powerbell.MainActivity;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.model.AppConfigBean;
|
||||
import cc.winboll.studio.powerbell.model.NotificationMessage;
|
||||
import cc.winboll.studio.powerbell.beans.AppConfigBean;
|
||||
import cc.winboll.studio.powerbell.beans.NotificationMessage;
|
||||
import cc.winboll.studio.powerbell.handlers.ControlCenterServiceHandler;
|
||||
import cc.winboll.studio.powerbell.receivers.ControlCenterServiceReceiver;
|
||||
import cc.winboll.studio.powerbell.services.AssistantService;
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package cc.winboll.studio.powerbell.unittest;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import android.widget.Button;
|
||||
import cc.winboll.studio.powerbell.MainActivity;
|
||||
import android.content.Intent;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/19 18:16
|
||||
* @Describe BackgroundViewTestFragment
|
||||
*/
|
||||
public class BackgroundViewTestFragment extends Fragment {
|
||||
|
||||
public static final String TAG = "BackgroundViewTestFragment";
|
||||
|
||||
View mainView;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
//super.onCreateView(inflater, container, savedInstanceState);
|
||||
|
||||
// 非调试状态就结束本线程
|
||||
if (!GlobalApplication.isDebugging()) {
|
||||
Thread.currentThread().destroy();
|
||||
}
|
||||
|
||||
mainView = inflater.inflate(R.layout.fragment_test_backgroundview, container, false);
|
||||
|
||||
((Button)mainView.findViewById(R.id.btn_main_activity)).setOnClickListener(new View.OnClickListener(){
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
|
||||
}
|
||||
});
|
||||
|
||||
ToastUtils.show(String.format("%s onCreate", TAG));
|
||||
return mainView;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,14 @@
|
||||
package cc.winboll.studio.powerbell.unittest;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.MainActivity;
|
||||
import cc.winboll.studio.libappbase.GlobalApplication;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
|
||||
import cc.winboll.studio.powerbell.utils.ImageCropUtils;
|
||||
import cc.winboll.studio.powerbell.views.BackgroundView;
|
||||
import java.io.File;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import android.nfc.tech.TagTechnology;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
@@ -27,157 +18,22 @@ import java.io.File;
|
||||
public class MainUnitTestActivity extends AppCompatActivity {
|
||||
|
||||
public static final String TAG = "MainUnitTestActivity";
|
||||
public static final int REQUEST_CROP_IMAGE = 0;
|
||||
// 新增:权限请求码
|
||||
public static final int REQUEST_STORAGE_PERMISSION = 1001;
|
||||
View mainView;
|
||||
BackgroundSourceUtils mBgSourceUtils;
|
||||
BackgroundView mBackgroundView;
|
||||
// 测试图片路径(用Environment获取,适配低版本,避免硬编码)
|
||||
String szTestSource = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
|
||||
+ "/PowerBell/unittest/2ae9dc9e-7a73-49d4-840a-7ff1712d868c1764798674763.jpeg";
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mBgSourceUtils = BackgroundSourceUtils.getInstance(this);
|
||||
mBgSourceUtils.loadSettings();
|
||||
|
||||
// 非调试状态就退出
|
||||
if (!GlobalApplication.isDebugging()) {
|
||||
finish();
|
||||
}
|
||||
setContentView(R.layout.activity_mainunittest);
|
||||
|
||||
mBackgroundView = findViewById(R.id.backgroundview);
|
||||
|
||||
((Button)findViewById(R.id.btn_main_activity)).setOnClickListener(new View.OnClickListener(){
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
startActivity(new Intent(MainUnitTestActivity.this, MainActivity.class));
|
||||
}
|
||||
});
|
||||
|
||||
// 裁剪测试按钮点击事件(新增权限校验)
|
||||
((Button)findViewById(R.id.btn_test_cropimage)).setOnClickListener(new View.OnClickListener(){
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
ToastUtils.show("onClick:准备启动裁剪");
|
||||
LogUtils.d(TAG, "【裁剪测试】点击裁剪按钮,校验权限");
|
||||
|
||||
// 修复1:移除高版本API依赖,适配低版本存储权限校验
|
||||
if (checkStoragePermission()) {
|
||||
// 权限已授予,启动裁剪
|
||||
startCropTest();
|
||||
} else {
|
||||
// 权限未授予,申请权限
|
||||
requestStoragePermission();
|
||||
}
|
||||
}
|
||||
});
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||
fragmentTransaction.add(R.id.activitymainunittestFrameLayout1, new BackgroundViewTestFragment(), BackgroundViewTestFragment.TAG);
|
||||
fragmentTransaction.commit();
|
||||
|
||||
ToastUtils.show(String.format("%s onCreate", TAG));
|
||||
// 加载测试图片(验证图片路径是否有效)
|
||||
loadBackground();
|
||||
ToastUtils.show(String.format("%s onCreate", TAG));
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动裁剪测试(抽取为单独方法,便于权限回调后调用)
|
||||
*/
|
||||
private void startCropTest() {
|
||||
// 修复2:输出路径用Environment获取,确保目录存在(避免路径无效)
|
||||
File outputDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
|
||||
+ "/PowerBell/unittest/");
|
||||
if (!outputDir.exists()) {
|
||||
outputDir.mkdirs(); // 创建目录(避免输出路径不存在导致裁剪失败)
|
||||
LogUtils.d(TAG, "【裁剪测试】创建输出目录:" + outputDir.getAbsolutePath());
|
||||
}
|
||||
String dstOutputPath = outputDir.getAbsolutePath()
|
||||
+ "/SelectCompress_2ae9dc9e-7a73-49d4-840a-7ff1712d868c1764798674763.jpeg";
|
||||
|
||||
// 修复3:自由裁剪时比例传0(避免100:100过大导致机型崩溃)
|
||||
ImageCropUtils.startImageCrop(
|
||||
MainUnitTestActivity.this,
|
||||
new File(szTestSource),
|
||||
new File(dstOutputPath),
|
||||
0, // 自由裁剪传0
|
||||
0, // 自由裁剪传0
|
||||
true,
|
||||
REQUEST_CROP_IMAGE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验存储读写权限(适配Android 6.0+ 低版本SDK,移除TIRAMISU依赖)
|
||||
*/
|
||||
private boolean checkStoragePermission() {
|
||||
// 适配Android 6.0(API 23)及以上,用通用的读写权限(移除高版本API)
|
||||
return ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED
|
||||
&& ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请存储读写权限(适配低版本SDK,移除READ_MEDIA_IMAGES依赖)
|
||||
*/
|
||||
private void requestStoragePermission() {
|
||||
LogUtils.d(TAG, "【裁剪测试】申请存储读写权限");
|
||||
// 用通用的读写权限(适配所有Android 6.0+ 机型,无高版本依赖)
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
||||
REQUEST_STORAGE_PERMISSION
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 权限申请回调
|
||||
*/
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == REQUEST_STORAGE_PERMISSION) {
|
||||
// 校验权限是否授予
|
||||
boolean allGranted = true;
|
||||
for (int result : grantResults) {
|
||||
if (result != PackageManager.PERMISSION_GRANTED) {
|
||||
allGranted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allGranted) {
|
||||
ToastUtils.show("存储权限已授予,启动裁剪");
|
||||
startCropTest(); // 权限授予后启动裁剪
|
||||
} else {
|
||||
ToastUtils.show("存储权限被拒绝,无法启动裁剪");
|
||||
LogUtils.e(TAG, "【裁剪测试】存储权限被拒绝");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
LogUtils.d(TAG, "【裁剪回调】requestCode:" + requestCode + ",resultCode:" + resultCode + ",data:" + (data == null ? "null" : data.toString()));
|
||||
ToastUtils.show(String.format("requestCode %d, resultCode %d, data is %s",requestCode, resultCode, data == null));
|
||||
// 裁剪完成后回收权限
|
||||
if (requestCode == REQUEST_CROP_IMAGE) {
|
||||
String dstOutputPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
|
||||
+ "/PowerBell/unittest/SelectCompress_2ae9dc9e-7a73-49d4-840a-7ff1712d868c1764798674763.jpeg";
|
||||
//Uri outputUri = ImageCropUtils.getFileProviderUriPublic(this, new File(dstOutputPath));
|
||||
//ImageCropUtils.releaseCropPermission(this, outputUri);
|
||||
mBackgroundView.loadImage(dstOutputPath);
|
||||
}
|
||||
}
|
||||
|
||||
void loadBackground() {
|
||||
// 校验测试图片是否存在(避免路径错误)
|
||||
File testFile = new File(szTestSource);
|
||||
if (testFile.exists() && testFile.length() > 100) {
|
||||
mBackgroundView.loadImage(szTestSource);
|
||||
LogUtils.d(TAG, "【图片加载】测试图片加载成功:" + szTestSource);
|
||||
} else {
|
||||
ToastUtils.show("测试图片不存在或无效");
|
||||
LogUtils.e(TAG, "【图片加载】测试图片无效:" + szTestSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.model.BatteryInfoBean;
|
||||
import cc.winboll.studio.powerbell.beans.BatteryInfoBean;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AppCacheUtils {
|
||||
|
||||
@@ -5,8 +5,8 @@ import android.content.Context;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.App;
|
||||
import cc.winboll.studio.powerbell.MainActivity;
|
||||
import cc.winboll.studio.powerbell.model.AppConfigBean;
|
||||
import cc.winboll.studio.powerbell.model.ControlCenterServiceBean;
|
||||
import cc.winboll.studio.powerbell.beans.AppConfigBean;
|
||||
import cc.winboll.studio.powerbell.beans.ControlCenterServiceBean;
|
||||
import cc.winboll.studio.powerbell.dialogs.YesNoAlertDialog;
|
||||
import cc.winboll.studio.powerbell.fragments.MainViewFragment;
|
||||
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
||||
@@ -79,7 +79,7 @@ public class AppConfigUtils {
|
||||
|
||||
@Override
|
||||
public void onNo() {
|
||||
MainActivity.relaodAppConfigs();
|
||||
MainViewFragment.relaodAppConfigs();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -183,7 +183,7 @@ public class AppConfigUtils {
|
||||
@Override
|
||||
public void onNo() {
|
||||
AppConfigUtils.getInstance(activity).loadAppConfigBean();
|
||||
MainActivity.relaodAppConfigs();
|
||||
MainViewFragment.relaodAppConfigs();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -198,6 +198,6 @@ public class AppConfigUtils {
|
||||
AppConfigBean.saveBean(mContext, mAppConfigBean);
|
||||
// 通知活动窗口和服务配置已更新
|
||||
ControlCenterService.updateStatus(mContext, mAppConfigBean);
|
||||
MainActivity.relaodAppConfigs();
|
||||
MainViewFragment.relaodAppConfigs();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2024/07/18 12:07:20
|
||||
* @Describe 背景图片工具集
|
||||
*/
|
||||
import android.content.Context;
|
||||
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
|
||||
import java.io.File;
|
||||
|
||||
public class BackgroundPictureUtils {
|
||||
|
||||
public static final String TAG = "BackgroundPictureUtils";
|
||||
|
||||
static BackgroundPictureUtils _mBackgroundPictureUtils;
|
||||
Context mContext;
|
||||
BackgroundPictureBean mBackgroundPictureBean;
|
||||
// 背景图片目录
|
||||
String mszBackgroundDir;
|
||||
|
||||
BackgroundPictureUtils(Context context) {
|
||||
mContext = context;
|
||||
String szExternalFilesDir = mContext.getExternalFilesDir(TAG) + File.separator;
|
||||
setBackgroundDir(szExternalFilesDir + "Background" + File.separator);
|
||||
loadBackgroundPictureBean();
|
||||
}
|
||||
|
||||
public static BackgroundPictureUtils getInstance(Context context) {
|
||||
if (_mBackgroundPictureUtils == null) {
|
||||
_mBackgroundPictureUtils = new BackgroundPictureUtils(context);
|
||||
}
|
||||
return _mBackgroundPictureUtils;
|
||||
}
|
||||
|
||||
//
|
||||
// 加载应用背景图片配置数据
|
||||
//
|
||||
public BackgroundPictureBean loadBackgroundPictureBean() {
|
||||
mBackgroundPictureBean = BackgroundPictureBean.loadBean(mContext, BackgroundPictureBean.class);
|
||||
if (mBackgroundPictureBean == null) {
|
||||
mBackgroundPictureBean = new BackgroundPictureBean();
|
||||
BackgroundPictureBean.saveBean(mContext, mBackgroundPictureBean);
|
||||
}
|
||||
return mBackgroundPictureBean;
|
||||
}
|
||||
|
||||
|
||||
void setBackgroundDir(String mszBackgroundDir) {
|
||||
this.mszBackgroundDir = mszBackgroundDir;
|
||||
}
|
||||
|
||||
public String getBackgroundDir() {
|
||||
return mszBackgroundDir;
|
||||
}
|
||||
|
||||
public BackgroundPictureBean getBackgroundPictureBean() {
|
||||
return mBackgroundPictureBean;
|
||||
}
|
||||
|
||||
public void saveData() {
|
||||
BackgroundPictureBean.saveBean(mContext, mBackgroundPictureBean);
|
||||
}
|
||||
}
|
||||
@@ -1,851 +0,0 @@
|
||||
package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.media.ExifInterface;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.BuildConfig;
|
||||
import cc.winboll.studio.powerbell.model.BackgroundBean;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.UUID;
|
||||
import android.os.Build;
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2024/07/18 12:07:20
|
||||
* @Describe 背景图片工具集(精简版:复用FileUtils,聚焦业务逻辑)
|
||||
*/
|
||||
public class BackgroundSourceUtils {
|
||||
|
||||
public static final String TAG = "BackgroundSourceUtils";
|
||||
// 裁剪相关常量(统一定义,避免硬编码)
|
||||
private static final String CROP_CACHE_DIR_NAME = "cache"; // 裁剪缓存目录(基础目录下)
|
||||
private static final String CROP_TEMP_FILE_NAME = "SourceCropTemp.jpg"; // 裁剪输入临时文件
|
||||
private static final String CROP_RESULT_FILE_NAME = "SourceCropped.jpg"; // 裁剪输出结果文件
|
||||
public static final String FILE_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider"; // 多包名兼容
|
||||
// 图片操作基础目录(核心:系统公共图片目录)
|
||||
private static final String PICTURE_BASE_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "PowerBell";
|
||||
// 新增:压缩图统一存储目录(图片基础目录下/BackgroundCrops)
|
||||
private static final String SOURCE_DIR_NAME = "BackgroundSource";
|
||||
private static final String COMPRESS_DIR_NAME = "BackgroundCompress";
|
||||
|
||||
// 1. 静态实例加volatile,禁止指令重排,保证可见性(双重校验锁单例核心)
|
||||
private static volatile BackgroundSourceUtils sInstance;
|
||||
private Context mContext;
|
||||
private File currentBackgroundBeanFile;
|
||||
private BackgroundBean currentBackgroundBean; // 正式Bean:独立实例
|
||||
private File previewBackgroundBeanFile;
|
||||
private BackgroundBean previewBackgroundBean; // 预览Bean:独立实例(与正式Bean完全分离)
|
||||
|
||||
// 2. 统一文件目录(分两类:图片目录→系统公共目录,JSON目录→应用外置存储)
|
||||
// 图片操作目录(系统公共目录:/storage/emulated/0/Pictures/PowerBell/)
|
||||
private File fPictureBaseDir; // 图片基础目录
|
||||
private File fCropCacheDir; // 裁剪缓存目录(基础目录下/cache)
|
||||
private File fBackgroundSourceDir; // 图片存储目录(基础目录下,存储正式/预览原图)
|
||||
private File fBackgroundCompressDir; // 新增:压缩图统一存储目录(基础目录下/BackgroundCrops)
|
||||
// JSON配置目录(原应用外置存储目录,不改变)
|
||||
private File fUtilsDir; // 工具类根目录(/Android/data/包名/files/BackgroundSourceUtils)
|
||||
private File fModelDir; // 模型文件目录(存储JSON配置)
|
||||
// 裁剪文件(统一放入图片基础目录下的cache)
|
||||
private File mCropSourceFile; // 裁剪临时文件(fCropCacheDir下)
|
||||
private File mCropResultFile; // 裁剪临时文件(fCropCacheDir下)
|
||||
|
||||
// 3. 私有构造器(加防反射逻辑+初始化所有目录/文件)
|
||||
private BackgroundSourceUtils(Context context) {
|
||||
// 防反射破坏:若已有实例,抛异常阻止重复创建
|
||||
if (sInstance != null) {
|
||||
throw new RuntimeException("BackgroundSourceUtils 是单例类,禁止重复创建!");
|
||||
}
|
||||
// 上下文用Application Context,避免Activity内存泄漏
|
||||
this.mContext = context.getApplicationContext();
|
||||
// 【核心调整1】实例化初期优先初始化所有必要目录(确保实例化完成时目录100%就绪)
|
||||
initNecessaryDirs();
|
||||
// 初始化所有文件(裁剪临时文件/结果文件等)
|
||||
initAllFiles();
|
||||
// 加载配置(确保正式/预览Bean是两份独立实例)
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
// 4. 双重校验锁单例(线程安全,高效,支持多线程并发调用,Java7语法兼容)
|
||||
public static BackgroundSourceUtils getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
synchronized (BackgroundSourceUtils.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new BackgroundSourceUtils(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 【核心新增】统一初始化所有必要目录(实例化初期调用,确保目录优先创建)
|
||||
* 整合图片目录+JSON目录,集中管理目录创建逻辑,保证实例化完成时所有目录就绪
|
||||
*/
|
||||
private void initNecessaryDirs() {
|
||||
LogUtils.d(TAG, "【实例化初期-目录初始化】开始创建所有必要目录...");
|
||||
// 1. 初始化图片操作目录(系统公共目录 /Pictures/PowerBell/)
|
||||
initPictureDirs();
|
||||
// 2. 初始化JSON配置目录(应用外置存储)
|
||||
initJsonDirs();
|
||||
LogUtils.d(TAG, "【实例化初期-目录初始化】所有必要目录创建完成!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化图片操作目录(核心:系统公共图片目录 /Pictures/PowerBell/,新增压缩图目录)
|
||||
* 【调整强化】新增目录创建后二次校验,失败则降级到备选目录,确保目录可用
|
||||
*/
|
||||
private void initPictureDirs() {
|
||||
// 1. 图片基础目录:/storage/emulated/0/Pictures/PowerBell
|
||||
fPictureBaseDir = new File(PICTURE_BASE_DIR);
|
||||
// 2. 图片存储目录:基础目录下(存储正式/预览原图)
|
||||
fBackgroundSourceDir = new File(fPictureBaseDir, SOURCE_DIR_NAME);
|
||||
// 3. 裁剪缓存目录:基础目录下/cache(所有裁剪操作在此目录)
|
||||
fCropCacheDir = new File(fPictureBaseDir, CROP_CACHE_DIR_NAME);
|
||||
// 4. 新增:压缩图统一存储目录(基础目录下/BackgroundCrops,所有压缩图放这里)
|
||||
fBackgroundCompressDir = new File(fPictureBaseDir, COMPRESS_DIR_NAME);
|
||||
|
||||
// 5. 强制创建所有图片目录(带二次校验+降级兜底)
|
||||
createDirWithPermission(fPictureBaseDir, "图片基础目录(" + PICTURE_BASE_DIR + ")");
|
||||
createDirWithPermission(fBackgroundSourceDir, "图片存储目录(基础目录下/" + SOURCE_DIR_NAME + ")");
|
||||
createDirWithPermission(fCropCacheDir, "裁剪缓存目录(基础目录/" + CROP_CACHE_DIR_NAME + ")");
|
||||
createDirWithPermission(fBackgroundCompressDir, "裁剪压缩图存储目录(基础目录/" + COMPRESS_DIR_NAME + ")");
|
||||
|
||||
// 6. 目录创建后最终校验(确保所有目录已就绪)
|
||||
validatePictureDirs();
|
||||
|
||||
LogUtils.d(TAG, "【图片目录初始化】完成:" +
|
||||
"基础目录=" + fPictureBaseDir.getAbsolutePath() +
|
||||
"图片存储目录=" + fBackgroundSourceDir.getAbsolutePath() +
|
||||
",裁剪缓存目录=" + fCropCacheDir.getAbsolutePath() +
|
||||
",裁剪压缩图存储目录=" + fBackgroundCompressDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化JSON配置目录(保留原逻辑:应用外置存储)
|
||||
* 【调整强化】新增目录创建后二次校验,失败则降级到应用内部缓存目录
|
||||
*/
|
||||
private void initJsonDirs() {
|
||||
// 1. 工具类根目录(应用外置存储)
|
||||
fUtilsDir = mContext.getExternalFilesDir(TAG);
|
||||
if (fUtilsDir == null) {
|
||||
LogUtils.e(TAG, "【JSON目录】应用外置存储不可用,切换到应用内部缓存目录");
|
||||
fUtilsDir = mContext.getDataDir();
|
||||
}
|
||||
// 2. 模型文件目录(存储JSON配置)
|
||||
fModelDir = new File(fUtilsDir, "ModelDir");
|
||||
// 强制创建JSON目录(带二次校验+降级兜底)
|
||||
createDirWithPermission(fModelDir, "JSON配置目录(应用外置存储)");
|
||||
|
||||
// 3. 初始化JSON文件对象(两份独立文件,对应两份Bean实例)
|
||||
currentBackgroundBeanFile = new File(fModelDir, "currentBackgroundBean.json");
|
||||
previewBackgroundBeanFile = new File(fModelDir, "previewBackgroundBean.json");
|
||||
|
||||
LogUtils.d(TAG, "【JSON目录初始化】完成:目录=" + fModelDir.getAbsolutePath() + ",正式JSON=" + currentBackgroundBeanFile.getName() + ",预览JSON=" + previewBackgroundBeanFile.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 【核心强化】创建目录并设置权限(适配系统公共目录/Pictures/PowerBell,确保实例化时目录就绪)
|
||||
* @param dir 要创建的目录
|
||||
* @param dirDesc 目录描述(用于日志打印)
|
||||
*/
|
||||
private void createDirWithPermission(File dir, String dirDesc) {
|
||||
if (dir == null) {
|
||||
LogUtils.e(TAG, "【文件管理】创建目录失败:目录对象为null(描述:" + dirDesc + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
// 第一步:主动检测并创建目录(递归创建所有父目录)
|
||||
if (!dir.exists()) {
|
||||
LogUtils.d(TAG, "【文件管理】" + dirDesc + "不存在,开始创建:" + dir.getAbsolutePath());
|
||||
dir.mkdirs(); // 递归创建所有父目录
|
||||
} else {
|
||||
LogUtils.d(TAG, "【文件管理】" + dirDesc + "已存在:" + dir.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 【新增】图片目录创建后最终校验(确保实例化时所有图片目录已就绪)
|
||||
*/
|
||||
private void validatePictureDirs() {
|
||||
LogUtils.d(TAG, "【图片目录校验】开始校验所有图片目录...");
|
||||
boolean allReady = true;
|
||||
if (!fPictureBaseDir.exists() || !fPictureBaseDir.isDirectory()) {
|
||||
LogUtils.e(TAG, "【图片目录校验】图片基础目录未就绪:" + fPictureBaseDir.getAbsolutePath());
|
||||
allReady = false;
|
||||
}
|
||||
if (!fBackgroundSourceDir.exists() || !fBackgroundSourceDir.isDirectory()) {
|
||||
LogUtils.e(TAG, "【图片目录校验】图片存储目录未就绪:" + fBackgroundSourceDir.getAbsolutePath());
|
||||
allReady = false;
|
||||
}
|
||||
if (!fCropCacheDir.exists() || !fCropCacheDir.isDirectory()) {
|
||||
LogUtils.e(TAG, "【图片目录校验】裁剪缓存目录未就绪:" + fCropCacheDir.getAbsolutePath());
|
||||
allReady = false;
|
||||
}
|
||||
if (!fBackgroundCompressDir.exists() || !fBackgroundCompressDir.isDirectory()) {
|
||||
LogUtils.e(TAG, "【图片目录校验】压缩图目录未就绪:" + fBackgroundCompressDir.getAbsolutePath());
|
||||
allReady = false;
|
||||
}
|
||||
if (allReady) {
|
||||
LogUtils.d(TAG, "【图片目录校验】所有图片目录均已就绪!");
|
||||
} else {
|
||||
LogUtils.e(TAG, "【图片目录校验】部分目录未就绪,可能影响后续功能!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化所有文件(裁剪文件→图片缓存目录,结果文件→图片存储目录)
|
||||
*/
|
||||
private void initAllFiles() {
|
||||
// 1. 裁剪临时文件
|
||||
//mCropSourceFile = new File(fCropCacheDir, CROP_TEMP_FILE_NAME);
|
||||
// 2. 裁剪结果文件
|
||||
//cropResultFile = new File(fCropCacheDir, CROP_RESULT_FILE_NAME);
|
||||
|
||||
// 新增:清理压缩图目录下的旧文件(避免残留)
|
||||
clearCropTempFiles();
|
||||
LogUtils.d(TAG, "【文件初始化】完成。");
|
||||
}
|
||||
|
||||
// 【核心实现】定义 getFileProviderUri 方法:将 File 转为 ContentUri(适配 FileProvider)
|
||||
public Uri getFileProviderUri(File file) {
|
||||
Log.d("BackgroundSourceUtils", "getFileProviderUri: 生成FileProvider Uri,文件路径:" + file.getAbsolutePath());
|
||||
Uri contentUri = null;
|
||||
try {
|
||||
// 适配 Android 7.0+:使用 FileProvider 生成 ContentUri(避免 FileUriExposedException)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
contentUri = FileProvider.getUriForFile(
|
||||
mContext,
|
||||
FILE_PROVIDER_AUTHORITY, // 与清单文件中一致
|
||||
file
|
||||
);
|
||||
Log.d("BackgroundSourceUtils", "getFileProviderUri: 7.0+ 生成ContentUri:" + contentUri.toString());
|
||||
} else {
|
||||
// 适配 Android 7.0 以下:直接使用 File.toURI()(兼容旧版本)
|
||||
contentUri = Uri.fromFile(file);
|
||||
Log.d("BackgroundSourceUtils", "getFileProviderUri: 7.0以下 生成FileUri:" + contentUri.toString());
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 捕获异常(如文件路径无效、授权不匹配等)
|
||||
Log.e("BackgroundSourceUtils", "getFileProviderUri: 生成Uri失败,异常:" + e.getMessage(), e);
|
||||
contentUri = null;
|
||||
}
|
||||
return contentUri;
|
||||
}
|
||||
|
||||
/*
|
||||
* 创建预览数据剪裁环境
|
||||
*/
|
||||
public boolean createAndUpdatePreviewEnvironmentForCropping(BackgroundBean oldPreviewBackgroundBean) {
|
||||
InputStream is = null;
|
||||
FileOutputStream fos = null;
|
||||
|
||||
try {
|
||||
clearCropTempFiles();
|
||||
Uri uri = UriUtil.getUriForFile(mContext, oldPreviewBackgroundBean.getBackgroundFilePath());
|
||||
//String szType = mContext.getContentResolver().getType(uri);
|
||||
// 2. 截取MIME类型后缀(如从image/jpeg中提取jpeg)【核心新增逻辑】
|
||||
String fileSuffix = FileUtils.getFileSuffix(mContext, uri);
|
||||
String newCropFileName = UUID.randomUUID().toString() + System.currentTimeMillis();
|
||||
mCropSourceFile = new File(fCropCacheDir, newCropFileName + "." + fileSuffix);
|
||||
mCropResultFile = new File(fCropCacheDir, "SelectCompress_" + newCropFileName + "." + fileSuffix);
|
||||
|
||||
if (FileUtils.isFileExists(oldPreviewBackgroundBean.getBackgroundScaledCompressFilePath())) {
|
||||
FileUtils.copyFile(new File(oldPreviewBackgroundBean.getBackgroundScaledCompressFilePath()), mCropResultFile);
|
||||
} else {
|
||||
mCropResultFile.createNewFile();
|
||||
}
|
||||
|
||||
if (FileUtils.isFileExists(oldPreviewBackgroundBean.getBackgroundFilePath())) {
|
||||
FileUtils.copyFile(new File(oldPreviewBackgroundBean.getBackgroundFilePath()), mCropSourceFile);
|
||||
} else {
|
||||
mCropSourceFile.createNewFile();
|
||||
// 1. 打开Uri输入流(兼容content:///file:// 等多种Uri格式)
|
||||
is = mContext.getContentResolver().openInputStream(uri);
|
||||
if (is == null) {
|
||||
LogUtils.e(TAG, "【选图解析】ContentResolver打开Uri失败,Uri:" + uri.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 初始化选图临时文件输出流(Java7 手动创建流,不依赖try-with-resources)
|
||||
fos = new FileOutputStream(mCropSourceFile);
|
||||
byte[] buffer = new byte[1024 * 8]; // 8KB缓冲区,平衡读写性能与内存占用
|
||||
int readLen; // 每次读取的字节长度
|
||||
|
||||
// 3. 流复制(Java7 标准while循环,避免Java8+语法)
|
||||
while ((readLen = is.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, readLen); // 精准写入读取到的字节,避免空字节填充
|
||||
}
|
||||
|
||||
// 4. 强制同步写入磁盘(解决Android 10+ 异步写入导致的文件无效问题)
|
||||
fos.flush();
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.getFD().sync(); // 确保数据写入物理磁盘,而非缓存
|
||||
} catch (IOException e) {
|
||||
LogUtils.w(TAG, "【选图解析】文件同步到磁盘失败,用flush()兜底:" + e.getMessage());
|
||||
fos.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 加载图片数据模型数据
|
||||
loadSettings();
|
||||
// 修改预览数据模型
|
||||
previewBackgroundBean.setBackgroundFileName(mCropSourceFile.getName());
|
||||
previewBackgroundBean.setBackgroundFilePath(mCropSourceFile.getAbsolutePath());
|
||||
|
||||
previewBackgroundBean.setBackgroundScaledCompressFileName(mCropResultFile.getName());
|
||||
previewBackgroundBean.setBackgroundScaledCompressFilePath(mCropResultFile.getAbsolutePath());
|
||||
// 保存数据模型数据更改
|
||||
saveSettings();
|
||||
|
||||
// 6. 解析成功日志(打印文件信息,便于问题排查)
|
||||
LogUtils.d(TAG, "【选图解析】Uri解析成功!");
|
||||
LogUtils.d(TAG, "→ 原Uri:" + uri.toString());
|
||||
LogUtils.d(TAG, "→ 图片剪裁数据源:" + mCropSourceFile.getAbsolutePath());
|
||||
LogUtils.d(TAG, "→ 图片剪裁数据源文件大小:" + mCropSourceFile.length() + " bytes");
|
||||
LogUtils.d(TAG, "→ 剪裁结果数据文件:" + mCropResultFile.getAbsolutePath());
|
||||
LogUtils.d(TAG, "→ 剪裁结果数据文件大小:" + mCropResultFile.length() + " bytes");
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
// 捕获所有异常(IO异常/空指针等),避免崩溃
|
||||
LogUtils.e(TAG, "【选图解析】流复制异常:" + e.getMessage(), e);
|
||||
// 异常时清理无效文件,防止残留
|
||||
clearCropTempFiles();
|
||||
return false;
|
||||
|
||||
} finally {
|
||||
// 7. 手动关闭流资源(Java7 标准写法,避免内存泄漏)
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【选图解析】输入流关闭失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【选图解析】输出流关闭失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载背景图片配置数据(核心:确保current/preview是两份独立的BackgroundBean实例)
|
||||
*/
|
||||
public void loadSettings() {
|
||||
// 1. 加载正式Bean(独立实例:从currentBackgroundBean.json加载,不存在则新建)
|
||||
currentBackgroundBean = BackgroundBean.loadBeanFromFile(currentBackgroundBeanFile.getAbsolutePath(), BackgroundBean.class);
|
||||
if (currentBackgroundBean == null) {
|
||||
currentBackgroundBean = new BackgroundBean(); // 正式Bean独立实例初始化
|
||||
BackgroundBean.saveBeanToFile(currentBackgroundBeanFile.getAbsolutePath(), currentBackgroundBean);
|
||||
LogUtils.d(TAG, "【配置管理】正式背景Bean不存在,创建独立实例并保存到JSON");
|
||||
}
|
||||
|
||||
// 2. 加载预览Bean(独立实例:从previewBackgroundBean.json加载,不存在则新建,与正式Bean完全分离)
|
||||
previewBackgroundBean = BackgroundBean.loadBeanFromFile(previewBackgroundBeanFile.getAbsolutePath(), BackgroundBean.class);
|
||||
if (previewBackgroundBean == null) {
|
||||
previewBackgroundBean = new BackgroundBean(); // 预览Bean独立实例初始化
|
||||
BackgroundBean.saveBeanToFile(previewBackgroundBeanFile.getAbsolutePath(), previewBackgroundBean);
|
||||
LogUtils.d(TAG, "【配置管理】预览背景Bean不存在,创建独立实例并保存到JSON");
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------ 对外提供的核心方法(路径已适配新目录)------------------------------
|
||||
public BackgroundBean getCurrentBackgroundBean() {
|
||||
return currentBackgroundBean;
|
||||
}
|
||||
|
||||
public BackgroundBean getPreviewBackgroundBean() {
|
||||
return previewBackgroundBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取正式背景图片路径(修复:移除每次 loadSettings(),避免 Bean 被覆盖;强化非空校验)
|
||||
*/
|
||||
public String getCurrentBackgroundFilePath() {
|
||||
String fileName = currentBackgroundBean.getBackgroundFileName();
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
LogUtils.e(TAG, "【路径管理】正式背景文件名为空,返回空路径");
|
||||
return "";
|
||||
}
|
||||
File file = new File(fBackgroundSourceDir, fileName);
|
||||
LogUtils.d(TAG, "【路径管理】正式背景路径:" + file.getAbsolutePath());
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览背景图片路径(修复:移除每次 loadSettings(),避免 Bean 被覆盖;强化非空校验)
|
||||
*/
|
||||
public String getPreviewBackgroundFilePath() {
|
||||
String fileName = previewBackgroundBean.getBackgroundFileName();
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
LogUtils.e(TAG, "【路径管理】预览背景文件名为空,返回空路径");
|
||||
return "";
|
||||
}
|
||||
File file = new File(fBackgroundSourceDir, fileName);
|
||||
LogUtils.d(TAG, "【路径管理】预览背景路径:" + file.getAbsolutePath());
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览背景压缩图片路径(同步修复:移除 loadSettings(),强化非空校验,统一指向BackgroundCrops目录)
|
||||
*/
|
||||
public String getPreviewBackgroundScaledCompressFilePath() {
|
||||
String compressFileName = previewBackgroundBean.getBackgroundScaledCompressFileName();
|
||||
if (TextUtils.isEmpty(compressFileName)) {
|
||||
LogUtils.e(TAG, "【路径管理】预览压缩背景文件名为空,返回空路径");
|
||||
return "";
|
||||
}
|
||||
// 关键:压缩图路径统一指向BackgroundCrops目录(不再用BackgroundSource)
|
||||
File file = new File(fBackgroundCompressDir, compressFileName);
|
||||
LogUtils.d(TAG, "【路径管理】预览压缩背景路径(BackgroundCrops目录):" + file.getAbsolutePath());
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增:获取正式背景压缩图片路径(统一指向BackgroundCrops目录,对外提供调用)
|
||||
*/
|
||||
public String getCurrentBackgroundScaledCompressFilePath() {
|
||||
String compressFileName = currentBackgroundBean.getBackgroundScaledCompressFileName();
|
||||
if (TextUtils.isEmpty(compressFileName)) {
|
||||
LogUtils.e(TAG, "【路径管理】正式压缩背景文件名为空,返回空路径");
|
||||
return "";
|
||||
}
|
||||
// 关键:压缩图路径统一指向BackgroundCrops目录
|
||||
File file = new File(fBackgroundCompressDir, compressFileName);
|
||||
LogUtils.d(TAG, "【路径管理】正式压缩背景路径(BackgroundCrops目录):" + file.getAbsolutePath());
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存配置(核心:将两份独立Bean实例,分别写入各自的JSON文件)
|
||||
*/
|
||||
public void saveSettings() {
|
||||
if (currentBackgroundBean != null && previewBackgroundBean != null) {
|
||||
BackgroundBean.saveBeanToFile(currentBackgroundBeanFile.getAbsolutePath(), currentBackgroundBean); // 正式Bean→正式JSON
|
||||
BackgroundBean.saveBeanToFile(previewBackgroundBeanFile.getAbsolutePath(), previewBackgroundBean); // 预览Bean→预览JSON
|
||||
LogUtils.d(TAG, "【配置管理】两份配置保存成功:正式JSON=" + currentBackgroundBeanFile.getAbsolutePath() + ",预览JSON=" + previewBackgroundBeanFile.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
LogUtils.d(TAG, "【配置管理】两份配置保存失败。currentBackgroundBean 与 previewBackgroundBean 有空值。");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片基础目录路径(对外提供:/Pictures/PowerBell/)
|
||||
*/
|
||||
public String getBackgroundSourceDirPath() {
|
||||
return fBackgroundSourceDir.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增:获取压缩图统一存储目录路径(对外提供:/Pictures/PowerBell/BackgroundCrops/)
|
||||
*/
|
||||
public String getBackgroundCompressDirPath() {
|
||||
return fBackgroundCompressDir.getAbsolutePath();
|
||||
}
|
||||
|
||||
public String getFileProviderAuthority() {
|
||||
return FILE_PROVIDER_AUTHORITY;
|
||||
}
|
||||
|
||||
// ------------------------------ 核心业务方法(复用FileUtils简化文件操作)------------------------------
|
||||
/**
|
||||
* 优化函数:仅裁剪结果图可保存到BackgroundSource(避免启动裁剪时误复制原图)
|
||||
* 说明:启动裁剪时不调用此方法,仅在裁剪完成后保存结果图时调用
|
||||
*/
|
||||
public BackgroundBean saveFileToPreviewBean(File sourceFile, String fileInfo) {
|
||||
final String TAG = "BackgroundSourceUtils";
|
||||
// 强化校验1:仅允许裁剪结果图传入(通过文件路径判断,避免原图误传入)
|
||||
if (sourceFile == null || !sourceFile.exists() || sourceFile.length() <= 0) {
|
||||
Log.e(TAG, "【保存优化】源文件无效,拒绝保存:" + (sourceFile != null ? sourceFile.getAbsolutePath() : "null"));
|
||||
return previewBackgroundBean;
|
||||
}
|
||||
|
||||
// 强化校验2:排除原图路径(避免启动裁剪时传入原图复制)
|
||||
String originalImageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath(); // 原图存储目录(如相册/拍照目录)
|
||||
if (sourceFile.getAbsolutePath().contains(originalImageDir)) {
|
||||
Log.w(TAG, "【保存优化】禁止复制原图到BackgroundSource,跳过保存");
|
||||
return previewBackgroundBean;
|
||||
}
|
||||
|
||||
// 确保BackgroundSource目录存在(实例化时已创建,此处二次确认)
|
||||
if (!fBackgroundSourceDir.exists()) {
|
||||
if (!fBackgroundSourceDir.mkdirs()) {
|
||||
Log.e(TAG, "【保存优化】BackgroundSource目录创建失败");
|
||||
return previewBackgroundBean;
|
||||
}
|
||||
}
|
||||
|
||||
// 生成唯一文件名(避免覆盖)
|
||||
String uniqueFileName = "bg_" + System.currentTimeMillis() + "_" + sourceFile.getName();
|
||||
File targetFile = new File(fBackgroundSourceDir, uniqueFileName);
|
||||
|
||||
// 执行复制(仅裁剪结果图会走到这一步)
|
||||
if (FileUtils.copyFile(sourceFile, targetFile)) {
|
||||
Log.d(TAG, "【保存优化】裁剪结果图保存成功:" + targetFile.getAbsolutePath());
|
||||
// 更新预览Bean(原有逻辑保留)
|
||||
previewBackgroundBean.setBackgroundFileName(uniqueFileName);
|
||||
previewBackgroundBean.setBackgroundFilePath(targetFile.getAbsolutePath());
|
||||
previewBackgroundBean.setBackgroundFileInfo(fileInfo);
|
||||
previewBackgroundBean.setIsUseBackgroundFile(true);
|
||||
// 保存Bean到本地(原有逻辑保留)
|
||||
saveSettings();
|
||||
} else {
|
||||
Log.e(TAG, "【保存优化】裁剪结果图复制失败:" + sourceFile.getAbsolutePath() + " → " + targetFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
return previewBackgroundBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交预览背景到正式背景(预览Bean → 正式Bean:深拷贝,新建正式Bean实例+逐字段拷贝)
|
||||
* 核心:深拷贝后,修改正式Bean不会影响预览Bean,两份实例完全独立,压缩图路径统一指向BackgroundCrops
|
||||
*/
|
||||
public void commitPreviewSourceToCurrent() {
|
||||
// 深拷贝第一步:新建正式Bean独立实例(彻底脱离预览Bean的引用)
|
||||
currentBackgroundBean = new BackgroundBean();
|
||||
// 深拷贝第二步:逐字段拷贝预览Bean的所有值(压缩图路径同步指向BackgroundCrops)
|
||||
currentBackgroundBean.setBackgroundFileName(previewBackgroundBean.getBackgroundFileName());
|
||||
currentBackgroundBean.setBackgroundFilePath(previewBackgroundBean.getBackgroundFilePath()); // 原图路径(BackgroundSource)
|
||||
currentBackgroundBean.setBackgroundFileInfo(previewBackgroundBean.getBackgroundFileInfo());
|
||||
currentBackgroundBean.setIsUseBackgroundFile(previewBackgroundBean.isUseBackgroundFile());
|
||||
currentBackgroundBean.setBackgroundScaledCompressFileName(previewBackgroundBean.getBackgroundScaledCompressFileName());
|
||||
currentBackgroundBean.setBackgroundScaledCompressFilePath(previewBackgroundBean.getBackgroundScaledCompressFilePath()); // 压缩图路径(BackgroundCrops)
|
||||
currentBackgroundBean.setIsUseBackgroundScaledCompressFile(previewBackgroundBean.isUseBackgroundScaledCompressFile()); // 重命名字段:拷贝压缩图启用状态
|
||||
currentBackgroundBean.setBackgroundWidth(previewBackgroundBean.getBackgroundWidth());
|
||||
currentBackgroundBean.setBackgroundHeight(previewBackgroundBean.getBackgroundHeight());
|
||||
currentBackgroundBean.setPixelColor(previewBackgroundBean.getPixelColor());
|
||||
|
||||
// 拷贝一份缓存图片文件到正式背景文件夹
|
||||
String previewFileName = previewBackgroundBean.getBackgroundFileName();
|
||||
String previewCropFileName = previewBackgroundBean.getBackgroundScaledCompressFileName();
|
||||
File previewFile = new File(previewBackgroundBean.getBackgroundFilePath());
|
||||
File previewCropFile = new File(previewBackgroundBean.getBackgroundScaledCompressFilePath());
|
||||
File currentFile = new File(fBackgroundSourceDir, previewFileName);
|
||||
File currentCropFile = new File(fBackgroundCompressDir, previewCropFileName);
|
||||
FileUtils.copyFile(previewFile, currentFile);
|
||||
FileUtils.copyFile(previewCropFile, currentCropFile);
|
||||
// 更新当前背景文件路径
|
||||
currentBackgroundBean.setBackgroundFilePath(currentFile.getAbsolutePath()); // 原图路径(BackgroundSource)
|
||||
currentBackgroundBean.setBackgroundScaledCompressFilePath(currentCropFile.getAbsolutePath()); // 压缩图路径(BackgroundCrops)
|
||||
|
||||
saveSettings(); // 分别保存:正式Bean→currentJSON,预览Bean→previewJSON(两份独立)
|
||||
LogUtils.d(TAG, "【配置管理】预览背景深拷贝到正式Bean:两份实例独立,压缩图统一存储到BackgroundCrops");
|
||||
ToastUtils.show("背景图片应用成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 将正式背景同步到预览背景(正式Bean → 预览Bean:深拷贝,新建预览Bean实例+逐字段拷贝)
|
||||
* 核心:深拷贝后,修改预览Bean不会影响正式Bean,两份实例完全独立,压缩图路径统一指向BackgroundCrops
|
||||
*/
|
||||
public void setCurrentSourceToPreview() {
|
||||
LogUtils.d(TAG, "正在初始化预览数据,setCurrentSourceToPreview()");
|
||||
// 深拷贝第一步:新建预览Bean独立实例(彻底脱离正式Bean的引用)
|
||||
previewBackgroundBean = new BackgroundBean();
|
||||
// 深拷贝第二步:逐字段拷贝正式Bean的所有值(压缩图路径同步指向BackgroundCrops)
|
||||
previewBackgroundBean.setBackgroundFileName(currentBackgroundBean.getBackgroundFileName());
|
||||
previewBackgroundBean.setBackgroundFilePath(currentBackgroundBean.getBackgroundFilePath()); // 原图路径(BackgroundSource)
|
||||
previewBackgroundBean.setBackgroundFileInfo(currentBackgroundBean.getBackgroundFileInfo());
|
||||
previewBackgroundBean.setIsUseBackgroundFile(currentBackgroundBean.isUseBackgroundFile());
|
||||
previewBackgroundBean.setBackgroundScaledCompressFileName(currentBackgroundBean.getBackgroundScaledCompressFileName());
|
||||
previewBackgroundBean.setBackgroundScaledCompressFilePath(currentBackgroundBean.getBackgroundScaledCompressFilePath()); // 压缩图路径(BackgroundCrops)
|
||||
previewBackgroundBean.setIsUseBackgroundScaledCompressFile(currentBackgroundBean.isUseBackgroundScaledCompressFile()); // 重命名字段:拷贝压缩图启用状态
|
||||
previewBackgroundBean.setBackgroundWidth(currentBackgroundBean.getBackgroundWidth());
|
||||
previewBackgroundBean.setBackgroundHeight(currentBackgroundBean.getBackgroundHeight());
|
||||
previewBackgroundBean.setPixelColor(currentBackgroundBean.getPixelColor());
|
||||
|
||||
saveSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* 工具方法:清理旧文件(避免文件锁定/残留,适配系统公共目录)【内部私有,不对外暴露】
|
||||
* @param file 要清理的文件
|
||||
* @param fileDesc 文件描述(用于日志打印)
|
||||
*/
|
||||
private void clearOldFile(File file, String fileDesc) {
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
LogUtils.w(TAG, "【文件管理】" + fileDesc + "已删除");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增:清理压缩图目录下的旧文件(避免残留,初始化时调用)
|
||||
*/
|
||||
void clearCropTempFiles() {
|
||||
for (File file : fCropCacheDir.listFiles()) {
|
||||
clearOldFile(file, "旧裁剪缓存文件(" + file.getAbsolutePath() + ")");
|
||||
}
|
||||
mCropSourceFile = null;
|
||||
mCropResultFile = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 适配原调用:mBgSourceUtils.copyFile(new File(""), parentDir)
|
||||
* 核心:复用FileUtils,支持「空源文件→仅创建目标目录」和「正常文件复制」两种场景
|
||||
* @param source 源文件(可为空/空文件,为空时仅创建目标目录)
|
||||
* @param target 目标文件/目录(若源文件为空,target视为目录并创建)
|
||||
* @return true=复制/创建成功,false=失败
|
||||
*/
|
||||
public boolean copyFile(File source, File target) {
|
||||
// 场景1:源文件为空(适配 new File("") 调用)→ 仅创建目标目录
|
||||
if (source == null || (source.exists() && source.length() <= 0) || TextUtils.isEmpty(source.getPath())) {
|
||||
if (target == null) {
|
||||
LogUtils.e(TAG, "【文件管理】目录创建失败:目标目录对象为null");
|
||||
return false;
|
||||
}
|
||||
// 若target是文件,取其父目录;若本身是目录,直接创建(实例化时已创建,此处二次确认)
|
||||
File targetDir = target.isFile() ? target.getParentFile() : target;
|
||||
createDirWithPermission(targetDir, "空源文件场景-目录创建(/Pictures/PowerBell下)");
|
||||
LogUtils.d(TAG, "【文件管理】空源文件场景:目录创建完成,路径=" + targetDir.getAbsolutePath());
|
||||
return true;
|
||||
}
|
||||
|
||||
// 场景2:正常文件复制(源文件非空且存在)→ 复用FileUtils.copyFile,确保高效兼容
|
||||
return FileUtils.copyFile(source, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 工具方法:获取目录类型描述(用于日志调试,明确目录类型,适配新目录结构)
|
||||
* @param dir 目标目录
|
||||
* @return 目录类型描述
|
||||
*/
|
||||
public String getDirTypeDesc(File dir) {
|
||||
if (dir == null) {
|
||||
return "未知目录(null)";
|
||||
}
|
||||
String dirPath = dir.getAbsolutePath();
|
||||
String publicPicturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
|
||||
String externalFilesPath = mContext.getExternalFilesDir(null) != null ? mContext.getExternalFilesDir(null).getAbsolutePath() : "";
|
||||
String cachePath = mContext.getCacheDir().getAbsolutePath();
|
||||
|
||||
if (!TextUtils.isEmpty(publicPicturePath)) {
|
||||
if (dirPath.contains(publicPicturePath + File.separator + "PowerBell" + File.separator + COMPRESS_DIR_NAME)) {
|
||||
return "系统公共图片目录(/Pictures/PowerBell/BackgroundCrops,压缩图统一存储目录)"; // 新增压缩图目录描述
|
||||
} else if (dirPath.contains(publicPicturePath + File.separator + "PowerBell")) {
|
||||
return "系统公共图片目录(/Pictures/PowerBell,图片存储/裁剪目录)";
|
||||
}
|
||||
} else if (!TextUtils.isEmpty(externalFilesPath) && dirPath.contains(externalFilesPath)) {
|
||||
return "应用私有外部目录(getExternalFilesDir(),JSON配置目录)";
|
||||
} else if (dirPath.contains(cachePath)) {
|
||||
return "应用内部缓存目录(getCacheDir(),兜底目录)";
|
||||
} else {
|
||||
return "外部存储目录(非应用私有,权限受限)";
|
||||
}
|
||||
return "未知目录";
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增:迁移旧压缩图路径到新目录(BackgroundCrops),兼容历史数据
|
||||
* @param bean 要迁移的BackgroundBean(正式/预览)
|
||||
* @param isCurrentBean 是否是正式Bean(用于日志区分)
|
||||
*/
|
||||
private void migrateCompressPathToNewDir(BackgroundBean bean, boolean isCurrentBean) {
|
||||
String oldCompressPath = bean.getBackgroundScaledCompressFilePath();
|
||||
String beanType = isCurrentBean ? "正式Bean" : "预览Bean";
|
||||
|
||||
// 校验:旧路径非空,且不在BackgroundCrops目录下,才需要迁移
|
||||
if (TextUtils.isEmpty(oldCompressPath) || oldCompressPath.contains(fBackgroundCompressDir.getAbsolutePath())) {
|
||||
LogUtils.d(TAG, "【路径迁移】" + beanType + "无需迁移:旧路径为空或已在BackgroundCrops目录");
|
||||
return;
|
||||
}
|
||||
|
||||
File oldCompressFile = new File(oldCompressPath);
|
||||
if (!oldCompressFile.exists() || !oldCompressFile.isFile() || oldCompressFile.length() <= 0) {
|
||||
LogUtils.w(TAG, "【路径迁移】" + beanType + "旧压缩文件无效,无需迁移:" + oldCompressPath);
|
||||
// 重置路径为新目录下的空文件(避免无效路径)
|
||||
String compressFileName = bean.getBackgroundScaledCompressFileName();
|
||||
if (!TextUtils.isEmpty(compressFileName)) {
|
||||
File newCompressFile = new File(fBackgroundCompressDir, compressFileName);
|
||||
bean.setBackgroundScaledCompressFilePath(newCompressFile.getAbsolutePath());
|
||||
saveSettings();
|
||||
LogUtils.d(TAG, "【路径迁移】" + beanType + "重置压缩路径到BackgroundCrops:" + newCompressFile.getAbsolutePath());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 迁移逻辑:复制旧文件到新目录,更新Bean路径,删除旧文件
|
||||
String compressFileName = bean.getBackgroundScaledCompressFileName();
|
||||
if (TextUtils.isEmpty(compressFileName)) {
|
||||
compressFileName = "ScaledCompress_" + System.currentTimeMillis() + ".jpg"; // 兜底生成文件名
|
||||
}
|
||||
File newCompressFile = new File(fBackgroundCompressDir, compressFileName);
|
||||
|
||||
// 复制旧文件到新目录
|
||||
boolean copySuccess = FileUtils.copyFile(oldCompressFile, newCompressFile);
|
||||
if (copySuccess) {
|
||||
// 更新Bean路径为新目录路径
|
||||
bean.setBackgroundScaledCompressFilePath(newCompressFile.getAbsolutePath());
|
||||
saveSettings();
|
||||
// 删除旧文件(清理残留)
|
||||
clearOldFile(oldCompressFile, beanType + "旧压缩文件(迁移后清理)");
|
||||
LogUtils.d(TAG, "【路径迁移】" + beanType + "压缩路径迁移成功:" + oldCompressPath + " → " + newCompressFile.getAbsolutePath());
|
||||
} else {
|
||||
LogUtils.e(TAG, "【路径迁移】" + beanType + "压缩文件复制失败,迁移终止:" + oldCompressPath);
|
||||
}
|
||||
}
|
||||
|
||||
// ======================================== 核心实现:获取图片旋转角度 ========================================
|
||||
/**
|
||||
* 读取图片EXIF信息,获取旋转角度(适配JPEG/PNG等主流格式)
|
||||
* @param imagePath 图片绝对路径(支持本地文件路径,兼容多包名临时目录)
|
||||
* @return 旋转角度(0/90/180/270,无旋转返回0)
|
||||
*/
|
||||
public int getImageRotateAngle(String imagePath) {
|
||||
// 1. 入参校验(避免空指针/无效路径)
|
||||
if (TextUtils.isEmpty(imagePath)) {
|
||||
Log.e(TAG, "getImageRotateAngle: 图片路径为空");
|
||||
return 0;
|
||||
}
|
||||
File imageFile = new File(imagePath);
|
||||
if (!imageFile.exists() || !imageFile.isFile() || imageFile.length() <= 0) {
|
||||
Log.e(TAG, "getImageRotateAngle: 图片文件无效,路径:" + imagePath);
|
||||
return 0;
|
||||
}
|
||||
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
// 2. 读取图片EXIF信息(优先用流读取,避免文件占用)
|
||||
inputStream = new FileInputStream(imageFile);
|
||||
ExifInterface exifInterface = new ExifInterface(inputStream);
|
||||
|
||||
// 3. 获取旋转角度标签(兼容不同设备的EXIF字段)
|
||||
int orientation = exifInterface.getAttributeInt(
|
||||
ExifInterface.TAG_ORIENTATION,
|
||||
ExifInterface.ORIENTATION_NORMAL
|
||||
);
|
||||
|
||||
// 4. 解析旋转角度(标准EXIF角度映射)
|
||||
switch (orientation) {
|
||||
case ExifInterface.ORIENTATION_ROTATE_90:
|
||||
return 90;
|
||||
case ExifInterface.ORIENTATION_ROTATE_180:
|
||||
return 180;
|
||||
case ExifInterface.ORIENTATION_ROTATE_270:
|
||||
return 270;
|
||||
default: // 正常/翻转等其他情况,均视为0度
|
||||
return 0;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// 兼容异常场景:如图片无EXIF信息、格式不支持(如WebP)
|
||||
Log.w(TAG, "getImageRotateAngle: 读取EXIF异常,路径:" + imagePath + ",错误:" + e.getMessage());
|
||||
return 0;
|
||||
} finally {
|
||||
// 5. 关闭流资源(避免内存泄漏/文件占用)
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "getImageRotateAngle: 流关闭失败,错误:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ======================================== 图片处理核心方法(压缩/裁剪/保存) ========================================
|
||||
/**
|
||||
* 压缩图片并保存(核心修复:路径非空校验+兜底路径,统一存储到BackgroundCrops目录)
|
||||
*/
|
||||
public void compressQualityToRecivedPicture(Bitmap bitmap) {
|
||||
// 兼容裁剪等旧调用:从工具类获取默认压缩路径(统一指向BackgroundCrops),转发至重载函数
|
||||
String defaultCompressPath = getPreviewBackgroundScaledCompressFilePath();
|
||||
compressQualityToRecivedPicture(bitmap, defaultCompressPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载方法:指定路径压缩图片并保存(修复:压缩后同步路径到预览Bean,统一存储到BackgroundCrops)
|
||||
* 适配场景:裁剪后生成压缩图,强制绑定路径到预览Bean,避免路径错位
|
||||
* @param bitmap 待压缩的Bitmap(裁剪后的缩放图)
|
||||
* @param targetCompressPath 强制指定的压缩目标路径(从预览Bean获取/生成,默认指向BackgroundCrops)
|
||||
*/
|
||||
public void compressQualityToRecivedPicture(Bitmap bitmap, String targetCompressPath) {
|
||||
LogUtils.d(TAG, "【压缩启动】开始压缩图片(指定路径),Bitmap状态:" + (bitmap != null && !bitmap.isRecycled()));
|
||||
|
||||
if (bitmap == null || bitmap.isRecycled()) {
|
||||
ToastUtils.show("压缩失败:图片为空");
|
||||
LogUtils.e(TAG, "【压缩失败】Bitmap为空或已回收");
|
||||
return;
|
||||
}
|
||||
|
||||
OutputStream outStream = null;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
LogUtils.d(TAG, "【压缩配置】目标路径(BackgroundCrops):" + targetCompressPath + ",Bitmap原始大小:" + bitmap.getByteCount() / 1024 + "KB");
|
||||
|
||||
File targetCompressFile = new File(targetCompressPath);
|
||||
if (targetCompressFile.exists()) {
|
||||
targetCompressFile.delete();
|
||||
}
|
||||
targetCompressFile.createNewFile();
|
||||
|
||||
// 写入压缩图(质量80,平衡清晰度和内存)
|
||||
fos = new FileOutputStream(targetCompressFile);
|
||||
outStream = new BufferedOutputStream(fos);
|
||||
boolean compressSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outStream);
|
||||
outStream.flush();
|
||||
// 强制同步到磁盘(避免异步写入导致控件读取不到文件)
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.getFD().sync();
|
||||
LogUtils.d(TAG, "【压缩保存】已强制同步到磁盘");
|
||||
} catch (IOException e) {
|
||||
LogUtils.w(TAG, "【压缩保存】sync()失败,flush()兜底:" + e.getMessage());
|
||||
outStream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
LogUtils.d(TAG, "【压缩结果】" + (compressSuccess ? "成功" : "失败") + ",大小:" + targetCompressFile.length() / 1024 + "KB,路径:" + targetCompressFile);
|
||||
|
||||
// 关键修复:压缩成功后,强制同步路径到预览Bean(双重保障,避免时序错位)
|
||||
if (compressSuccess) {
|
||||
ToastUtils.show("图片压缩成功");
|
||||
} else {
|
||||
ToastUtils.show("图片压缩失败");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【压缩异常】IO错误:" + e.getMessage(), e);
|
||||
ToastUtils.show("图片压缩失败");
|
||||
} finally {
|
||||
// 资源回收(避免内存泄漏)
|
||||
if (outStream != null) {
|
||||
try {
|
||||
outStream.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【流关闭失败】BufferedOutputStream:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【流关闭失败】FileOutputStream:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
if (bitmap != null && !bitmap.isRecycled()) {
|
||||
bitmap.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/12/08 21:11
|
||||
* @Describe 把 R.drawable 中的图片保存为 File 对象的工具类
|
||||
* 适配 PowerBell 项目:支持指定保存路径、自动创建目录、处理PNG图片压缩
|
||||
*/
|
||||
public class DrawableToFileUtils {
|
||||
private static final String TAG = "DrawableToFileUtils";
|
||||
|
||||
/**
|
||||
* 核心方法:将 R.drawable 图片保存为 File 对象
|
||||
* @param context 上下文(用于获取 Resources)
|
||||
* @param drawableResId 图片资源ID(如 R.drawable.ic_test_png)
|
||||
* @param fileName 保存的文件名(需带 .png 后缀,如 "test_drawable.png")
|
||||
* @return 保存成功返回 File 对象,失败返回 null
|
||||
*/
|
||||
public static File saveDrawableToFile(Context context, int drawableResId, String filePath) {
|
||||
// 1. 校验参数(避免空指针/无效参数)
|
||||
if (context == null || drawableResId == 0 || filePath == null || filePath.isEmpty()) {
|
||||
LogUtils.e(TAG, "【保存失败】参数无效(context为空/资源ID为0/文件名为空)");
|
||||
return null;
|
||||
}
|
||||
if (!filePath.endsWith(".png")) {
|
||||
filePath += ".png"; // 强制添加 .png 后缀,确保图片格式正确
|
||||
LogUtils.d(TAG, "【格式适配】自动添加.png后缀,最终文件名:" + filePath);
|
||||
}
|
||||
|
||||
// 3. 构建目标 File 对象(最终保存的文件路径)
|
||||
File targetFile = new File(filePath);
|
||||
LogUtils.d(TAG, "【保存路径】目标文件路径:" + targetFile.getAbsolutePath());
|
||||
|
||||
// 4. 读取 drawable 资源为 Bitmap(处理高清图/缩放问题)
|
||||
Bitmap bitmap = null;
|
||||
try {
|
||||
// 读取 drawable 资源(适配不同分辨率的图片,避免变形)
|
||||
bitmap = BitmapFactory.decodeResource(context.getResources(), drawableResId);
|
||||
if (bitmap == null) {
|
||||
LogUtils.e(TAG, "【读取失败】无法读取drawable资源(资源ID:" + drawableResId + ")");
|
||||
return null;
|
||||
}
|
||||
LogUtils.d(TAG, "【读取成功】drawable资源转Bitmap成功(宽:" + bitmap.getWidth() + ",高:" + bitmap.getHeight() + ")");
|
||||
|
||||
// 5. 将 Bitmap 写入 File(PNG格式,无损保存)
|
||||
FileOutputStream fos = new FileOutputStream(targetFile);
|
||||
// 压缩参数:PNG格式,质量100(无损),写入输出流
|
||||
boolean isSaved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
||||
fos.flush(); // 刷新输出流
|
||||
fos.close(); // 关闭输出流
|
||||
|
||||
// 6. 校验保存结果(文件是否存在且有效)
|
||||
if (isSaved && targetFile.exists() && targetFile.length() > 100) {
|
||||
LogUtils.d(TAG, "【保存成功】drawable图片保存为File:" + targetFile.getAbsolutePath());
|
||||
return targetFile; // 保存成功,返回File对象
|
||||
} else {
|
||||
LogUtils.e(TAG, "【保存失败】图片写入文件无效(文件大小:" + (targetFile.exists() ? targetFile.length() : 0) + "字节)");
|
||||
// 保存失败,删除无效文件
|
||||
if (targetFile.exists()) {
|
||||
targetFile.delete();
|
||||
LogUtils.d(TAG, "【清理无效文件】已删除无效文件:" + targetFile.getAbsolutePath());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【保存异常】写入文件时出错:" + e.getMessage());
|
||||
LogUtils.e(TAG, "【异常堆栈】" + Log.getStackTraceString(e));
|
||||
return null;
|
||||
} finally {
|
||||
// 回收Bitmap资源(避免内存溢出)
|
||||
if (bitmap != null && !bitmap.isRecycled()) {
|
||||
bitmap.recycle();
|
||||
LogUtils.d(TAG, "【资源回收】Bitmap资源已回收");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载方法:自定义保存路径(灵活适配不同场景)
|
||||
* @param context 上下文
|
||||
* @param drawableResId 图片资源ID
|
||||
* @param saveDirPath 自定义保存目录路径(如 "/storage/emulated/0/PowerBell/custom/")
|
||||
* @param fileName 保存的文件名(带.png后缀)
|
||||
* @return 保存成功返回File对象,失败返回null
|
||||
*/
|
||||
public static File saveDrawableToFile(Context context, int drawableResId, String saveDirPath, String fileName) {
|
||||
File filePath = new File(saveDirPath, fileName);
|
||||
return saveDrawableToFile(context, drawableResId, filePath.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,286 +1,176 @@
|
||||
package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.UUID;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
/**
|
||||
* 文件操作工具类
|
||||
* 功能:文件读写、复制、图片转换、文件名处理等常用文件操作
|
||||
* 适配:Java 7+,支持Android全版本
|
||||
* 注意:调用文件操作前需确保已获取存储权限(Android 6.0+ 需动态申请)
|
||||
* 文件读取工具类
|
||||
*/
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
/** 日志标签 */
|
||||
public static final String TAG = "FileUtils";
|
||||
/** 读取文件默认缓冲区大小(10KB) */
|
||||
private static final int BUFFER_SIZE = 10240;
|
||||
/** 最大读取文件大小(1GB),防止OOM */
|
||||
private static final long MAX_READ_FILE_SIZE = 1024 * 1024 * 1024;
|
||||
|
||||
// ====================================== 文件读取相关 ======================================
|
||||
|
||||
/**
|
||||
* 读取文件内容并转为字符串
|
||||
* @param filePath 文件绝对路径(非空)
|
||||
* @return 文件内容字符串
|
||||
* @throws IOException 异常:文件不存在、文件过大、读取失败等
|
||||
*/
|
||||
//
|
||||
// 读取文件内容,作为字符串返回
|
||||
//
|
||||
public static String readFileAsString(String filePath) throws IOException {
|
||||
// 1. 校验文件合法性
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("文件不存在:" + filePath);
|
||||
}
|
||||
if (file.length() > MAX_READ_FILE_SIZE) {
|
||||
throw new IOException("文件过大(超过1GB),禁止读取:" + filePath);
|
||||
}
|
||||
throw new FileNotFoundException(filePath);
|
||||
}
|
||||
|
||||
// 2. 读取文件内容(使用StringBuilder高效拼接)
|
||||
StringBuilder sb = new StringBuilder((int) file.length());
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int readLen;
|
||||
// 循环读取缓冲区,避免一次性读取大文件导致OOM
|
||||
while ((readLen = fis.read(buffer)) > 0) {
|
||||
sb.append(new String(buffer, 0, readLen));
|
||||
}
|
||||
}
|
||||
if (file.length() > 1024 * 1024 * 1024) {
|
||||
throw new IOException("File is too large");
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder((int) (file.length()));
|
||||
// 创建字节输入流
|
||||
FileInputStream fis = new FileInputStream(filePath);
|
||||
// 创建一个长度为10240的Buffer
|
||||
byte[] bbuf = new byte[10240];
|
||||
// 用于保存实际读取的字节数
|
||||
int hasRead = 0;
|
||||
while ((hasRead = fis.read(bbuf)) > 0) {
|
||||
sb.append(new String(bbuf, 0, hasRead));
|
||||
}
|
||||
fis.close();
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件内容并转为byte数组(适用于二进制文件:图片、音频等)
|
||||
* @param filePath 文件绝对路径(非空)
|
||||
* @return 文件内容byte数组
|
||||
* @throws IOException 异常:文件不存在、读取失败等
|
||||
*/
|
||||
//
|
||||
// 根据文件路径读取byte[] 数组
|
||||
//
|
||||
public static byte[] readFileByBytes(String filePath) throws IOException {
|
||||
// 1. 校验文件合法性
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("文件不存在:" + filePath);
|
||||
}
|
||||
throw new FileNotFoundException(filePath);
|
||||
} else {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
|
||||
BufferedInputStream in = null;
|
||||
|
||||
// 2. 缓冲流读取(高效,减少IO次数)
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
|
||||
try {
|
||||
in = new BufferedInputStream(new FileInputStream(file));
|
||||
short bufSize = 1024;
|
||||
byte[] buffer = new byte[bufSize];
|
||||
int len1;
|
||||
while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
|
||||
bos.write(buffer, 0, len1);
|
||||
}
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int readLen;
|
||||
while ((readLen = bis.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, readLen);
|
||||
byte[] var7 = bos.toByteArray();
|
||||
return var7;
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (IOException var14) {
|
||||
var14.printStackTrace();
|
||||
}
|
||||
|
||||
bos.close();
|
||||
}
|
||||
bos.flush();
|
||||
return bos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================== 文件复制相关 ======================================
|
||||
|
||||
/**
|
||||
* 基于FileChannel复制文件(高效,适用于大文件复制)
|
||||
* @param source 源文件(非空,必须存在)
|
||||
* @param dest 目标文件(非空,父目录会自动创建)
|
||||
* @throws IOException 异常:源文件不存在、复制失败等
|
||||
*/
|
||||
//
|
||||
// 文件复制函数
|
||||
//
|
||||
public static void copyFileUsingFileChannels(File source, File dest) throws IOException {
|
||||
// 1. 校验源文件合法性
|
||||
if (!source.exists() || !source.isFile()) {
|
||||
throw new FileNotFoundException("源文件不存在或不是文件:" + source.getAbsolutePath());
|
||||
}
|
||||
|
||||
// 2. 创建目标文件父目录
|
||||
if (!dest.getParentFile().exists()) {
|
||||
dest.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
// 3. 通道复制(try-with-resources 自动关闭通道,无需手动关闭)
|
||||
try (FileChannel inputChannel = new FileInputStream(source).getChannel();
|
||||
FileChannel outputChannel = new FileOutputStream(dest).getChannel()) {
|
||||
// 从输入通道复制到输出通道(高效,底层优化)
|
||||
FileChannel inputChannel = null;
|
||||
FileChannel outputChannel = null;
|
||||
try {
|
||||
inputChannel = new FileInputStream(source).getChannel();
|
||||
outputChannel = new FileOutputStream(dest).getChannel();
|
||||
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
|
||||
LogUtils.d(TAG, "文件复制成功(FileChannel):" + source.getAbsolutePath() + " → " + dest.getAbsolutePath());
|
||||
} finally {
|
||||
inputChannel.close();
|
||||
outputChannel.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 简化版文件复制(基于NIO Files工具类,代码简洁,适用于中小文件)
|
||||
* @param oldFile 源文件(非空,必须存在)
|
||||
* @param newFile 目标文件(非空,父目录会自动创建)
|
||||
* @return 复制结果:true-成功,false-失败
|
||||
* 将文件生成位图
|
||||
* @param path
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean copyFile(File oldFile, File newFile) {
|
||||
// 1. 校验源文件合法性
|
||||
if (oldFile == null || !oldFile.exists() || !oldFile.isFile()) {
|
||||
LogUtils.e(TAG, "源文件无效:" + (oldFile != null ? oldFile.getAbsolutePath() : "null"));
|
||||
return false;
|
||||
public static BitmapDrawable getImageDrawable(String path)
|
||||
throws IOException {
|
||||
//打开文件
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. 创建目标文件父目录
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
int BUFFER_SIZE = 1000;
|
||||
byte[] bt = new byte[BUFFER_SIZE];
|
||||
|
||||
//得到文件的输入流
|
||||
InputStream in = new FileInputStream(file);
|
||||
|
||||
//将文件读出到输出流中
|
||||
int readLength = in.read(bt);
|
||||
while (readLength != -1) {
|
||||
outStream.write(bt, 0, readLength);
|
||||
readLength = in.read(bt);
|
||||
}
|
||||
|
||||
//转换成byte 后 再格式化成位图
|
||||
byte[] data = outStream.toByteArray();
|
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// 生成位图
|
||||
BitmapDrawable bd = new BitmapDrawable(bitmap);
|
||||
|
||||
return bd;
|
||||
}
|
||||
|
||||
public static boolean copyFile(File oldFile, File newFile) {
|
||||
//String oldPath = "path/to/original/file.txt";
|
||||
//String newPath = "path/to/new-location/for/file.txt";
|
||||
|
||||
//File oldFile = new java.io.File(oldPath);
|
||||
//File newFile = new java.io.File(newPath);
|
||||
if (!newFile.getParentFile().exists()) {
|
||||
newFile.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
// 3. 复制文件(覆盖已有目标文件)
|
||||
try {
|
||||
Path sourcePath = Paths.get(oldFile.getPath());
|
||||
Path destPath = Paths.get(newFile.getPath());
|
||||
// 先删除已有目标文件(避免覆盖失败)
|
||||
if (newFile.exists()) {
|
||||
newFile.delete();
|
||||
}
|
||||
Files.copy(sourcePath, destPath);
|
||||
LogUtils.d(TAG, "文件复制成功(Files):" + oldFile.getAbsolutePath() + " → " + newFile.getAbsolutePath());
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "文件复制失败:" + e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================== 图片文件相关 ======================================
|
||||
|
||||
/**
|
||||
* 从文件路径获取BitmapDrawable(适用于Android图片显示)
|
||||
* @param path 图片文件绝对路径(非空)
|
||||
* @return BitmapDrawable 图片对象(文件不存在/读取失败返回null)
|
||||
* @throws IOException 异常:文件读取IO错误
|
||||
*/
|
||||
public static BitmapDrawable getImageDrawable(String path) throws IOException {
|
||||
// 1. 校验文件合法性
|
||||
File file = new File(path);
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
LogUtils.e(TAG, "图片文件不存在:" + path);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. 读取文件并转为BitmapDrawable(缓冲流读取,减少内存占用)
|
||||
try (InputStream is = new FileInputStream(file);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int readLen;
|
||||
while ((readLen = is.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, readLen);
|
||||
}
|
||||
|
||||
// 3. 生成Bitmap并包装为BitmapDrawable
|
||||
byte[] imageBytes = bos.toByteArray();
|
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
|
||||
return new BitmapDrawable(bitmap);
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================== 文件名处理相关 ======================================
|
||||
|
||||
/**
|
||||
* 截取文件后缀名(兼容多 "." 场景,如"image.2025.png" → ".png")
|
||||
* @param file 目标文件(可为null)
|
||||
* @return 文件后缀名:带点(如".jpg"),无后缀/文件无效返回空字符串
|
||||
*/
|
||||
public static String getFileSuffixWithMultiDot(File file) {
|
||||
// 1. 校验文件合法性
|
||||
if (file == null || !file.isFile()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// 2. 提取文件名并查找最后一个 "."
|
||||
String fileName = file.getName();
|
||||
int lastDotIndex = fileName.lastIndexOf(".");
|
||||
|
||||
// 3. 校验后缀合法性(排除无后缀、以点结尾、后缀过长的异常文件)
|
||||
if (lastDotIndex == -1 // 无 "."
|
||||
|| lastDotIndex == fileName.length() - 1 // 以 "." 结尾(如".gitignore")
|
||||
|| (fileName.length() - lastDotIndex) > 5) { // 后缀长度超过5(异常文件名)
|
||||
return "";
|
||||
}
|
||||
|
||||
// 4. 返回小写后缀(统一格式,避免大小写不一致问题)
|
||||
return fileName.substring(lastDotIndex).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一文件名(优化版:唯一、合法、简洁)
|
||||
* 生成规则:UUID(去掉"-") + "_" + 时间戳 + 原文件后缀
|
||||
* @param refFile 参考文件(用于提取后缀名,可为null)
|
||||
* @return 唯一文件名(如"a1b2c3d4e5f6_1730000000000.jpg",无后缀则不带点)
|
||||
*/
|
||||
public static String createUniqueFileName(File refFile) {
|
||||
// 1. 获取参考文件的后缀名(自动容错null/无效文件)
|
||||
String suffix = getFileSuffixWithMultiDot(refFile);
|
||||
|
||||
// 2. 生成唯一标识(UUID确保全局唯一,时间戳进一步降低重复概率)
|
||||
String uniqueId = UUID.randomUUID().toString().replace("-", ""); // 去掉"-"简化文件名
|
||||
long timeStamp = System.currentTimeMillis();
|
||||
|
||||
// 3. 拼接文件名(分场景处理,避免多余点)
|
||||
if (suffix.isEmpty()) {
|
||||
// 无后缀:唯一ID + 时间戳
|
||||
return String.format("%s_%d", uniqueId, timeStamp);
|
||||
if (!oldFile.exists()) {
|
||||
//System.out.println("The original file does not exist.");
|
||||
LogUtils.d(TAG, "The original file does not exist.");
|
||||
} else {
|
||||
// 有后缀:唯一ID + 时间戳 + 后缀(无多余点)
|
||||
return String.format("%s_%d%s", uniqueId, timeStamp, suffix);
|
||||
try {
|
||||
// 源文件路径
|
||||
Path sourcePath = Paths.get(oldFile.getPath());
|
||||
// 目标文件路径
|
||||
Path destPath = Paths.get(newFile.getPath());
|
||||
if(newFile.exists()) {
|
||||
newFile.delete();
|
||||
}
|
||||
Files.copy(sourcePath, destPath);
|
||||
LogUtils.d(TAG, "File copy successfully.");
|
||||
//System.out.println("File moved successfully.");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||
//System.err.println("An error occurred while moving the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制输入流到文件(兼容Uri解析失败场景)
|
||||
*/
|
||||
public static void copyStreamToFile(InputStream inputStream, File file) throws IOException {
|
||||
if (inputStream == null || file == null) {
|
||||
throw new IllegalArgumentException("InputStream或File不能为空");
|
||||
}
|
||||
File parentDir = file.getParentFile();
|
||||
if (!parentDir.exists() && !parentDir.mkdirs()) {
|
||||
throw new IOException("无法创建父目录:" + parentDir.getAbsolutePath());
|
||||
}
|
||||
try {
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, length);
|
||||
}
|
||||
outputStream.flush();
|
||||
} finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
LogUtils.e("FileUtils", "关闭输入流失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFileSuffix(Context context, Uri uri){
|
||||
String szType = context.getContentResolver().getType(uri);
|
||||
// 2. 截取MIME类型后缀(如从image/jpeg中提取jpeg)【核心新增逻辑】
|
||||
String fileSuffix = "";
|
||||
if (szType != null && szType.contains("/")) {
|
||||
// 分割字符串,取"/"后面的部分(如"image/jpeg" → 分割后取索引1的"jpeg")
|
||||
fileSuffix = szType.split("/")[1];
|
||||
// 调试日志:打印截取后的文件后缀
|
||||
} else {
|
||||
// 异常处理:若类型为空或格式错误,默认后缀设为jpeg(保留原逻辑兼容性)
|
||||
fileSuffix = "jpeg";
|
||||
}
|
||||
return fileSuffix;
|
||||
}
|
||||
|
||||
public static boolean isFileExists(String path) {
|
||||
File file = new File(path);
|
||||
return file.exists();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import androidx.core.content.FileProvider;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.model.BackgroundBean;
|
||||
import com.yalantis.ucrop.UCrop;
|
||||
import com.yalantis.ucrop.UCropActivity;
|
||||
import java.io.File;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
|
||||
/**
|
||||
* 图片裁剪工具类(集成uCrop,脱离系统依赖)
|
||||
*/
|
||||
public class ImageCropUtils {
|
||||
public static final String TAG = "ImageCropUtils";
|
||||
// FileProvider 授权(与项目一致)
|
||||
private static final String FILE_PROVIDER_SUFFIX = ".fileprovider";
|
||||
|
||||
/**
|
||||
* 启动uCrop裁剪(核心方法,替代系统裁剪)
|
||||
* @param activity 上下文
|
||||
* @param inputFile 输入图片文件
|
||||
* @param outputFile 输出图片文件
|
||||
* @param isFreeCrop 是否自由裁剪(true=自由,false=固定比例)
|
||||
* @param requestCode 裁剪请求码
|
||||
*/
|
||||
public static void startImageCrop(Activity activity,
|
||||
File inputFile,
|
||||
File outputFile,
|
||||
int aspectX,
|
||||
int aspectY,
|
||||
boolean isFreeCrop,
|
||||
int requestCode) {
|
||||
// 校验输入参数
|
||||
if (activity == null || activity.isFinishing()) {
|
||||
LogUtils.e(TAG, "【裁剪异常】上下文Activity无效");
|
||||
return;
|
||||
}
|
||||
if (inputFile == null || !inputFile.exists() || inputFile.length() <= 100) {
|
||||
LogUtils.e(TAG, "【裁剪异常】输入文件无效");
|
||||
showToast(activity, "无有效图片可裁剪");
|
||||
return;
|
||||
}
|
||||
if (outputFile == null) {
|
||||
LogUtils.e(TAG, "【裁剪异常】输出文件路径为空");
|
||||
showToast(activity, "裁剪输出路径无效");
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成输入/输出Uri(适配FileProvider)
|
||||
Uri inputUri = getFileProviderUri(activity, inputFile);
|
||||
Uri outputUri = Uri.fromFile(outputFile); // uCrop 支持直接用文件Uri(兼容低版本)
|
||||
|
||||
// 配置uCrop参数
|
||||
UCrop uCrop = UCrop.of(inputUri, outputUri);
|
||||
UCrop.Options options = new UCrop.Options();
|
||||
|
||||
// 裁剪模式配置(自由裁剪/固定比例)
|
||||
if (isFreeCrop) {
|
||||
// 自由裁剪:无固定比例,可随意调整
|
||||
uCrop.withAspectRatio(0, 0);
|
||||
options.setFreeStyleCropEnabled(true); // 开启自由裁剪
|
||||
} else {
|
||||
// 固定比例(默认1:1,可根据需求修改)
|
||||
uCrop.withAspectRatio(aspectX, aspectY);
|
||||
options.setFreeStyleCropEnabled(false);
|
||||
}
|
||||
|
||||
// 裁剪配置(优化体验)
|
||||
options.setCompressionFormat(android.graphics.Bitmap.CompressFormat.JPEG); // 输出格式
|
||||
options.setCompressionQuality(100); // 图片质量
|
||||
options.setHideBottomControls(true); // 隐藏底部控制栏(简化界面)
|
||||
options.setToolbarTitle("图片裁剪"); // 工具栏标题
|
||||
options.setToolbarColor(activity.getResources().getColor(R.color.colorPrimary)); // 工具栏颜色(适配项目主题)
|
||||
options.setStatusBarColor(activity.getResources().getColor(R.color.colorPrimaryDark)); // 状态栏颜色
|
||||
|
||||
// 应用配置并启动裁剪
|
||||
uCrop.withOptions(options);
|
||||
// 启动uCrop裁剪Activity(替代系统裁剪)
|
||||
uCrop.start(activity, requestCode);
|
||||
|
||||
LogUtils.d(TAG, "【uCrop启动】成功,输入Uri:" + inputUri + ",输出Uri:" + outputUri + ",请求码:" + requestCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载方法:适配BackgroundBean
|
||||
*/
|
||||
public static void startImageCrop(Activity activity,
|
||||
BackgroundBean cropBean,
|
||||
int aspectX,
|
||||
int aspectY,
|
||||
boolean isFreeCrop,
|
||||
int requestCode) {
|
||||
File inputFile = new File(cropBean.getBackgroundFilePath());
|
||||
File outputFile = new File(cropBean.getBackgroundScaledCompressFilePath());
|
||||
startImageCrop(activity, inputFile, outputFile, aspectX, aspectY, isFreeCrop, requestCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成FileProvider Uri
|
||||
*/
|
||||
private static Uri getFileProviderUri(Activity activity, File file) {
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
String authority = activity.getPackageName() + FILE_PROVIDER_SUFFIX;
|
||||
Uri uri = FileProvider.getUriForFile(activity, authority, file);
|
||||
LogUtils.d(TAG, "【Uri生成】FileProvider Uri:" + uri);
|
||||
return uri;
|
||||
} else {
|
||||
Uri uri = Uri.fromFile(file);
|
||||
LogUtils.d(TAG, "【Uri生成】普通Uri:" + uri);
|
||||
return uri;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "【Uri生成】失败:" + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理uCrop裁剪回调(在Activity的onActivityResult中调用)
|
||||
* @param requestCode 请求码
|
||||
* @param resultCode 结果码
|
||||
* @param data 回调数据
|
||||
* @return 裁剪成功返回输出文件路径,失败返回null
|
||||
*/
|
||||
public static String handleCropResult(int requestCode, int resultCode, Intent data, int cropRequestCode) {
|
||||
// 校验是否是uCrop的回调
|
||||
if (requestCode == cropRequestCode) {
|
||||
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||
// 裁剪成功,获取输出Uri
|
||||
Uri outputUri = UCrop.getOutput(data);
|
||||
if (outputUri != null) {
|
||||
String outputPath = outputUri.getPath();
|
||||
LogUtils.d(TAG, "【uCrop回调】裁剪成功,输出路径:" + outputPath);
|
||||
return outputPath;
|
||||
}
|
||||
} else if (resultCode == UCrop.RESULT_ERROR) {
|
||||
// 裁剪失败,获取异常信息
|
||||
Throwable error = UCrop.getError(data);
|
||||
LogUtils.e(TAG, "【uCrop回调】裁剪失败:" + (error != null ? error.getMessage() : "未知错误"));
|
||||
} else {
|
||||
LogUtils.d(TAG, "【uCrop回调】裁剪被取消");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示Toast
|
||||
*/
|
||||
private static void showToast(Activity activity, String msg) {
|
||||
if (activity != null && !activity.isFinishing()) {
|
||||
android.widget.Toast.makeText(activity, msg, android.widget.Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 暴露getFileProviderUri方法(供外部调用)
|
||||
*/
|
||||
public static Uri getFileProviderUriPublic(Activity activity, File file) {
|
||||
return getFileProviderUri(activity, file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import android.widget.RemoteViews;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import cc.winboll.studio.powerbell.MainActivity;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import cc.winboll.studio.powerbell.model.NotificationMessage;
|
||||
import cc.winboll.studio.powerbell.beans.NotificationMessage;
|
||||
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
||||
|
||||
public class NotificationUtils2 {
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/12/01 16:05
|
||||
* @Describe 权限申请工具类(单例)
|
||||
* 核心特性:
|
||||
* 1. 适配全Android版本(6.0+ 动态权限 / 13+ 兼容)
|
||||
* 2. 支持多包名场景(无硬编码包名)
|
||||
* 3. 统一权限校验、申请、回调处理
|
||||
* 4. 自带用户引导(拒绝权限+不再询问场景)
|
||||
*/
|
||||
public class PermissionUtils {
|
||||
public static final String TAG = "PermissionUtils";
|
||||
// 存储权限请求码(与Activity保持一致,避免冲突)
|
||||
public static final int STORAGE_PERMISSION_REQUEST2 = 100;
|
||||
public static final int REQUEST_MANAGE_EXTERNAL_STORAGE = 101;
|
||||
|
||||
// 单例实例(双重校验锁,线程安全)
|
||||
private static volatile PermissionUtils sInstance;
|
||||
|
||||
// 私有构造(禁止外部实例化)
|
||||
private PermissionUtils() {}
|
||||
|
||||
/**
|
||||
* 获取单例实例(适配多包名,无硬编码)
|
||||
*/
|
||||
public static PermissionUtils getInstance() {
|
||||
if (sInstance == null) {
|
||||
synchronized (PermissionUtils.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new PermissionUtils();
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
// ======================================== 存储权限核心方法 ========================================
|
||||
/**
|
||||
* 检查并申请存储权限(统一入口,适配全Android版本)
|
||||
* @param activity 上下文(用于权限申请和弹窗)
|
||||
* @return true:权限已全部获取;false:需要申请权限
|
||||
*/
|
||||
public boolean checkAndRequestStoragePermission(Activity activity) {
|
||||
if (activity == null || activity.isFinishing()) {
|
||||
LogUtils.e(TAG, "【权限检查】Activity为空或已销毁,权限检查失败");
|
||||
return false;
|
||||
}
|
||||
LogUtils.d(TAG, "【权限检查】开始检查存储权限,Android版本:" + Build.VERSION.SDK_INT);
|
||||
|
||||
// 统一使用 WRITE_EXTERNAL_STORAGE + READ_EXTERNAL_STORAGE(适配所有版本,避免READ_MEDIA_IMAGES找不到符号)
|
||||
String[] requiredPermissions = {
|
||||
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
android.Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
};
|
||||
|
||||
// 筛选未授予的权限
|
||||
List<String> needPermissions = new ArrayList<>();
|
||||
for (String permission : requiredPermissions) {
|
||||
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
needPermissions.add(permission);
|
||||
}
|
||||
}
|
||||
|
||||
// 无权限需要申请:触发动态申请
|
||||
if (!needPermissions.isEmpty()) {
|
||||
String[] permissionsArr = needPermissions.toArray(new String[0]);
|
||||
ActivityCompat.requestPermissions(activity, permissionsArr, STORAGE_PERMISSION_REQUEST2);
|
||||
LogUtils.d(TAG, "【权限申请】已触发存储权限申请:" + TextUtils.join(",", permissionsArr));
|
||||
return false;
|
||||
}
|
||||
|
||||
// 所有权限已授予
|
||||
LogUtils.d(TAG, "【权限检查】存储权限已全部获取");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理存储权限申请回调(统一逻辑,无需在Activity中重复编写)
|
||||
* @param activity 上下文
|
||||
* @param requestCode 请求码(匹配STORAGE_PERMISSION_REQUEST)
|
||||
* @param permissions 申请的权限数组
|
||||
* @param grantResults 权限授予结果数组
|
||||
* @return true:回调已处理;false:非当前工具类的权限回调
|
||||
*/
|
||||
public boolean handleStoragePermissionResult(Activity activity, int requestCode, String[] permissions, int[] grantResults) {
|
||||
// 过滤非存储权限回调
|
||||
if (requestCode != STORAGE_PERMISSION_REQUEST2) {
|
||||
return false;
|
||||
}
|
||||
LogUtils.d(TAG, "【权限回调】处理存储权限回调,requestCode:" + requestCode);
|
||||
|
||||
if (activity == null || activity.isFinishing()) {
|
||||
LogUtils.e(TAG, "【权限回调】Activity为空或已销毁,回调处理终止");
|
||||
return true;
|
||||
}
|
||||
|
||||
// 校验所有权限是否授予
|
||||
boolean allGranted = true;
|
||||
for (int grantResult : grantResults) {
|
||||
if (grantResult != PackageManager.PERMISSION_GRANTED) {
|
||||
allGranted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (allGranted) {
|
||||
// 全部授予:提示用户重新操作
|
||||
ToastUtils.show(activity.getString(R.string.permission_grant_success));
|
||||
LogUtils.d(TAG, "【权限回调】所有存储权限已授予");
|
||||
} else {
|
||||
// 部分/全部拒绝:判断是否勾选“不再询问”
|
||||
boolean shouldShowRationale = false;
|
||||
for (String permission : permissions) {
|
||||
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
|
||||
shouldShowRationale = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldShowRationale) {
|
||||
// 未勾选“不再询问”:弹窗引导重新申请
|
||||
showPermissionRationaleDialog(activity);
|
||||
} else {
|
||||
// 已勾选“不再询问”:引导用户去设置页开启
|
||||
showPermissionSettingDialog(activity);
|
||||
}
|
||||
LogUtils.d(TAG, "【权限回调】部分/全部存储权限被拒绝,是否需要引导:" + shouldShowRationale);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ======================================== 辅助方法(私有,封装细节) ========================================
|
||||
/**
|
||||
* 弹窗:未勾选“不再询问”时,提示用户授予权限
|
||||
*/
|
||||
private void showPermissionRationaleDialog(final Activity activity) {
|
||||
new AlertDialog.Builder(activity)
|
||||
.setTitle(activity.getString(R.string.permission_title))
|
||||
.setMessage(activity.getString(R.string.permission_storage_rationale))
|
||||
.setPositiveButton(activity.getString(R.string.confirm), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
// 重新申请权限
|
||||
checkAndRequestStoragePermission(activity);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(activity.getString(R.string.cancel), null)
|
||||
.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* 弹窗:已勾选“不再询问”时,引导用户去应用设置页开启权限
|
||||
*/
|
||||
private void showPermissionSettingDialog(final Activity activity) {
|
||||
new AlertDialog.Builder(activity)
|
||||
.setTitle(activity.getString(R.string.permission_denied_title))
|
||||
.setMessage(activity.getString(R.string.permission_storage_setting_guide))
|
||||
.setPositiveButton(activity.getString(R.string.go_to_setting), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
// 跳转应用设置页(适配多包名,动态获取当前包名)
|
||||
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
|
||||
intent.setData(uri);
|
||||
activity.startActivity(intent);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(activity.getString(R.string.cancel), null)
|
||||
.show();
|
||||
}
|
||||
|
||||
// ======================================== 扩展:其他权限方法(可选) ========================================
|
||||
/**
|
||||
* 检查单个权限是否已授予(通用方法,可复用)
|
||||
*/
|
||||
public boolean isPermissionGranted(Activity activity, String permission) {
|
||||
if (activity == null || TextUtils.isEmpty(permission)) {
|
||||
return false;
|
||||
}
|
||||
return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请单个权限(通用方法,可复用)
|
||||
*/
|
||||
public void requestSinglePermission(Activity activity, String permission, int requestCode) {
|
||||
if (activity == null || TextUtils.isEmpty(permission)) {
|
||||
return;
|
||||
}
|
||||
if (!isPermissionGranted(activity, permission)) {
|
||||
ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
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&豆包大模型<zhangsken@qq.com>
|
||||
* @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); // 下载失败(子线程回调)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package cc.winboll.studio.powerbell.utils;
|
||||
|
||||
import cc.winboll.studio.powerbell.model.BatteryInfoBean;
|
||||
import cc.winboll.studio.powerbell.beans.BatteryInfoBean;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
@@ -103,11 +103,6 @@ public class UriUtil {
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public static Uri getUriForFile(Context context, String filePath) {
|
||||
File file = new File(filePath);
|
||||
return getUriForFile(context, file);
|
||||
}
|
||||
|
||||
public static Uri getUriForFile(Context context, File file) {
|
||||
//Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
|
||||
|
||||
@@ -4,176 +4,327 @@ import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.text.TextUtils;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ImageView.ScaleType;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.model.BackgroundBean;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
/**
|
||||
* 基于Java7的BackgroundView(LinearLayout+ImageView,保持原图比例居中平铺)
|
||||
* 核心:ImageView保持原图比例,在LinearLayout中居中平铺,无拉伸、无裁剪
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/11/19 18:01
|
||||
* @Describe 背景图片视图控件(支持预览临时图片 + 外部刷新)
|
||||
*/
|
||||
public class BackgroundView extends RelativeLayout {
|
||||
|
||||
public static final String TAG = "BackgroundView";
|
||||
|
||||
private Context mContext;
|
||||
private LinearLayout mLlContainer; // 主容器LinearLayout
|
||||
private ImageView mIvBackground; // 图片显示控件
|
||||
private float mImageAspectRatio = 1.0f; // 原图宽高比(宽/高)
|
||||
Context mContext;
|
||||
private ImageView ivBackground;
|
||||
|
||||
private static String BACKGROUND_IMAGE_FOLDER = "Background";
|
||||
private static String BACKGROUND_IMAGE_FILENAME = "current.data";
|
||||
private static String BACKGROUND_IMAGE_PREVIEW_FILENAME = "current_preview.data";
|
||||
private static String backgroundSourceFilePath;
|
||||
private float imageAspectRatio = 1.0f; // 默认 1:1
|
||||
// 标记当前是否处于预览状态
|
||||
private boolean isPreviewMode = false;
|
||||
|
||||
// ====================================== 构造器(Java7兼容) ======================================
|
||||
public BackgroundView(Context context) {
|
||||
super(context);
|
||||
LogUtils.d(TAG, "=== BackgroundView 构造器1 启动 ===");
|
||||
this.mContext = context;
|
||||
initView();
|
||||
}
|
||||
|
||||
public BackgroundView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
LogUtils.d(TAG, "=== BackgroundView 构造器2 启动 ===");
|
||||
this.mContext = context;
|
||||
initView();
|
||||
}
|
||||
|
||||
public BackgroundView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
LogUtils.d(TAG, "=== BackgroundView 构造器3 启动 ===");
|
||||
this.mContext = context;
|
||||
initView();
|
||||
}
|
||||
|
||||
// ====================================== 初始化 ======================================
|
||||
private void initView() {
|
||||
LogUtils.d(TAG, "=== initView 启动 ===");
|
||||
// 1. 配置当前控件:全屏+透明
|
||||
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
||||
setBackgroundColor(0x00000000);
|
||||
setBackground(new ColorDrawable(0x00000000));
|
||||
|
||||
// 2. 初始化主容器LinearLayout
|
||||
initLinearLayout();
|
||||
|
||||
// 3. 初始化ImageView
|
||||
initImageView();
|
||||
|
||||
// 4. 初始设置透明背景
|
||||
setDefaultTransparentBackground();
|
||||
LogUtils.d(TAG, "=== initView 完成 ===");
|
||||
public BackgroundView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
this.mContext = context;
|
||||
initView();
|
||||
}
|
||||
|
||||
private void initLinearLayout() {
|
||||
LogUtils.d(TAG, "=== initLinearLayout 启动 ===");
|
||||
mLlContainer = new LinearLayout(mContext);
|
||||
// 配置LinearLayout:全屏+垂直方向+居中
|
||||
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.MATCH_PARENT
|
||||
);
|
||||
mLlContainer.setLayoutParams(llParams);
|
||||
mLlContainer.setOrientation(LinearLayout.VERTICAL);
|
||||
mLlContainer.setGravity(android.view.Gravity.CENTER); // 子View居中
|
||||
mLlContainer.setBackgroundColor(0x00000000);
|
||||
this.addView(mLlContainer);
|
||||
LogUtils.d(TAG, "=== initLinearLayout 完成 ===");
|
||||
void initView() {
|
||||
initBackgroundImageView();
|
||||
initBackgroundImagePath();
|
||||
loadAndSetImageViewBackground();
|
||||
}
|
||||
|
||||
private void initImageView() {
|
||||
LogUtils.d(TAG, "=== initImageView 启动 ===");
|
||||
mIvBackground = new ImageView(mContext);
|
||||
// 配置ImageView:wrap_content+居中+透明背景
|
||||
LinearLayout.LayoutParams ivParams = new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT,
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
mIvBackground.setLayoutParams(ivParams);
|
||||
mIvBackground.setScaleType(ScaleType.FIT_CENTER); // 保持比例+居中平铺
|
||||
mIvBackground.setBackgroundColor(0x00000000);
|
||||
mLlContainer.addView(mIvBackground);
|
||||
LogUtils.d(TAG, "=== initImageView 完成 ===");
|
||||
private void initBackgroundImageView() {
|
||||
ivBackground = new ImageView(mContext);
|
||||
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
|
||||
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
|
||||
ivBackground.setLayoutParams(layoutParams);
|
||||
ivBackground.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||
this.addView(ivBackground);
|
||||
}
|
||||
|
||||
public void loadBackgroundBean(BackgroundBean bean) {
|
||||
if(!bean.isUseBackgroundFile()) {
|
||||
setDefaultTransparentBackground();
|
||||
return;
|
||||
}
|
||||
if(bean.isUseBackgroundScaledCompressFile()) {
|
||||
loadImage(bean.getBackgroundScaledCompressFilePath());
|
||||
} else {
|
||||
private void initBackgroundImagePath() {
|
||||
File externalFilesDir = mContext.getExternalFilesDir(null);
|
||||
if (externalFilesDir == null) {
|
||||
LogUtils.e(TAG, "外置存储不可用,无法初始化背景图片路径");
|
||||
return;
|
||||
}
|
||||
|
||||
loadImage(bean.getBackgroundFilePath());
|
||||
}
|
||||
}
|
||||
File backgroundDir = new File(externalFilesDir, BACKGROUND_IMAGE_FOLDER);
|
||||
if (!backgroundDir.exists()) {
|
||||
backgroundDir.mkdirs();
|
||||
}
|
||||
backgroundSourceFilePath = new File(backgroundDir, BACKGROUND_IMAGE_FILENAME).getAbsolutePath();
|
||||
}
|
||||
|
||||
// ====================================== 对外方法 ======================================
|
||||
/**
|
||||
* 加载图片(保持原图比例,在LinearLayout中居中平铺)
|
||||
* @param imagePath 图片绝对路径
|
||||
* 拷贝图片文件到背景资源目录(正式背景)
|
||||
*/
|
||||
public void loadImage(String imagePath) {
|
||||
LogUtils.d(TAG, "=== loadImage 启动,路径:" + imagePath + " ===");
|
||||
if (TextUtils.isEmpty(imagePath)) {
|
||||
setDefaultTransparentBackground();
|
||||
public void saveToBackgroundSources(String srcBackgroundPath) {
|
||||
initBackgroundImagePath();
|
||||
if (backgroundSourceFilePath == null) {
|
||||
LogUtils.e(TAG, "目标路径初始化失败,无法保存背景图片");
|
||||
return;
|
||||
}
|
||||
|
||||
File imageFile = new File(imagePath);
|
||||
if (!imageFile.exists() || !imageFile.isFile()) {
|
||||
LogUtils.e(TAG, "图片文件无效");
|
||||
setDefaultTransparentBackground();
|
||||
File srcFile = new File(srcBackgroundPath);
|
||||
if (!srcFile.exists() || !srcFile.isFile()) {
|
||||
LogUtils.e(TAG, String.format("源文件不存在或不是文件:%s", srcBackgroundPath));
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算原图比例
|
||||
if (!calculateImageAspectRatio(imageFile)) {
|
||||
setDefaultTransparentBackground();
|
||||
return;
|
||||
File destFile = new File(backgroundSourceFilePath);
|
||||
File destDir = destFile.getParentFile();
|
||||
if (destDir != null && !destDir.exists()) {
|
||||
boolean isDirCreated = destDir.mkdirs();
|
||||
if (!isDirCreated) {
|
||||
LogUtils.e(TAG, "目标目录创建失败:" + destDir.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 压缩加载Bitmap
|
||||
Bitmap bitmap = decodeBitmapWithCompress(imageFile, 1080, 1920);
|
||||
if (bitmap == null) {
|
||||
setDefaultTransparentBackground();
|
||||
return;
|
||||
}
|
||||
FileInputStream fis = null;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fis = new FileInputStream(srcFile);
|
||||
fos = new FileOutputStream(destFile);
|
||||
|
||||
// 设置图片
|
||||
mIvBackground.setImageDrawable(new BitmapDrawable(mContext.getResources(), bitmap));
|
||||
adjustImageViewSize(); // 调整尺寸
|
||||
LogUtils.d(TAG, "=== loadImage 完成 ===");
|
||||
byte[] buffer = new byte[4096];
|
||||
int len;
|
||||
while ((len = fis.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, len);
|
||||
}
|
||||
fos.flush();
|
||||
|
||||
LogUtils.d(TAG, String.format("文件拷贝成功:%s -> %s", srcBackgroundPath, backgroundSourceFilePath));
|
||||
// 拷贝成功后,若处于预览模式则退出预览,加载正式背景
|
||||
if (isPreviewMode) {
|
||||
exitPreviewMode();
|
||||
} else {
|
||||
loadAndSetImageViewBackground();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, String.format("文件拷贝失败:%s", e.getMessage()), e);
|
||||
if (destFile.exists()) {
|
||||
destFile.delete();
|
||||
LogUtils.d(TAG, "已删除损坏的目标文件");
|
||||
}
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "输入流关闭失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "输出流关闭失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================== 内部工具方法 ======================================
|
||||
/**
|
||||
* 【新增公共函数】预览临时图片(不修改正式背景文件)
|
||||
* @param previewImagePath 临时预览图片的路径
|
||||
*/
|
||||
public void previewBackgroundImage(String previewImagePath) {
|
||||
if (previewImagePath == null || previewImagePath.isEmpty()) {
|
||||
LogUtils.e(TAG, "预览图片路径为空");
|
||||
return;
|
||||
}
|
||||
|
||||
File previewFile = new File(previewImagePath);
|
||||
if (!previewFile.exists() || !previewFile.isFile()) {
|
||||
LogUtils.e(TAG, "预览图片不存在或不是文件:" + previewImagePath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算预览图片宽高比
|
||||
if (!calculateImageAspectRatio(previewFile)) {
|
||||
LogUtils.e(TAG, "预览图片尺寸无效,无法预览");
|
||||
return;
|
||||
}
|
||||
|
||||
// 压缩加载预览图片
|
||||
Bitmap previewBitmap = decodeBitmapWithCompress(previewFile, 1080, 1920);
|
||||
if (previewBitmap == null) {
|
||||
LogUtils.e(TAG, "预览图片加载失败");
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置预览图片到 ImageView
|
||||
Drawable previewDrawable = new BitmapDrawable(mContext.getResources(), previewBitmap);
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
|
||||
ivBackground.setBackground(previewDrawable);
|
||||
} else {
|
||||
ivBackground.setBackgroundDrawable(previewDrawable);
|
||||
}
|
||||
|
||||
// 调整 ImageView 尺寸以匹配预览图片宽高比
|
||||
adjustImageViewSize();
|
||||
isPreviewMode = true;
|
||||
LogUtils.d(TAG, "进入预览模式,预览图片路径:" + previewImagePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 【新增公共函数】退出预览模式,恢复显示正式背景图片
|
||||
*/
|
||||
public void exitPreviewMode() {
|
||||
if (isPreviewMode) {
|
||||
loadAndSetImageViewBackground();
|
||||
isPreviewMode = false;
|
||||
LogUtils.d(TAG, "退出预览模式,恢复正式背景");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共函数:供外部类调用,重新加载正式背景图片(刷新显示)
|
||||
*/
|
||||
public void reloadBackgroundImage() {
|
||||
LogUtils.d(TAG, "外部调用重新加载背景图片");
|
||||
initBackgroundImagePath();
|
||||
loadAndSetImageViewBackground();
|
||||
// 若处于预览模式,退出预览
|
||||
if (isPreviewMode) {
|
||||
isPreviewMode = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载正式背景图片并设置到 ImageView
|
||||
*/
|
||||
private void loadAndSetImageViewBackground() {
|
||||
if (backgroundSourceFilePath == null) {
|
||||
setDefaultImageViewBackground();
|
||||
return;
|
||||
}
|
||||
|
||||
File backgroundFile = new File(backgroundSourceFilePath);
|
||||
if (!backgroundFile.exists() || !backgroundFile.isFile()) {
|
||||
LogUtils.e(TAG, "背景图片不存在:" + backgroundSourceFilePath);
|
||||
setDefaultImageViewBackground();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!calculateImageAspectRatio(backgroundFile)) {
|
||||
setDefaultImageViewBackground();
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bitmap = decodeBitmapWithCompress(backgroundFile, 1080, 1920);
|
||||
if (bitmap == null) {
|
||||
LogUtils.e(TAG, "图片加载失败,无法解析为 Bitmap");
|
||||
setDefaultImageViewBackground();
|
||||
return;
|
||||
}
|
||||
|
||||
Drawable backgroundDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
|
||||
ivBackground.setBackground(backgroundDrawable);
|
||||
} else {
|
||||
ivBackground.setBackgroundDrawable(backgroundDrawable);
|
||||
}
|
||||
|
||||
adjustImageViewSize();
|
||||
LogUtils.d(TAG, "ImageView 背景加载成功,宽高比:" + imageAspectRatio);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算图片宽高比(宽/高)
|
||||
*/
|
||||
private boolean calculateImageAspectRatio(File file) {
|
||||
try {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
||||
|
||||
int width = options.outWidth;
|
||||
int height = options.outHeight;
|
||||
if (width <= 0 || height <= 0) {
|
||||
LogUtils.e(TAG, "图片尺寸无效");
|
||||
int imageWidth = options.outWidth;
|
||||
int imageHeight = options.outHeight;
|
||||
|
||||
if (imageWidth <= 0 || imageHeight <= 0) {
|
||||
LogUtils.e(TAG, "图片尺寸无效:宽=" + imageWidth + ", 高=" + imageHeight);
|
||||
return false;
|
||||
}
|
||||
|
||||
mImageAspectRatio = (float) width / height;
|
||||
LogUtils.d(TAG, "原图比例:" + mImageAspectRatio);
|
||||
imageAspectRatio = (float) imageWidth / imageHeight;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "计算比例失败:" + e.getMessage());
|
||||
LogUtils.e(TAG, "计算图片宽高比失败:" + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态调整 ImageView 尺寸以匹配图片宽高比
|
||||
*/
|
||||
private void adjustImageViewSize() {
|
||||
int parentWidth = getWidth();
|
||||
int parentHeight = getHeight();
|
||||
|
||||
if (parentWidth == 0 || parentHeight == 0) {
|
||||
post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
adjustImageViewSize();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
int imageViewWidth, imageViewHeight;
|
||||
if (imageAspectRatio >= 1.0f) { // 横图
|
||||
imageViewWidth = Math.min(parentWidth, (int) (parentHeight * imageAspectRatio));
|
||||
imageViewHeight = (int) (imageViewWidth / imageAspectRatio);
|
||||
} else { // 竖图
|
||||
imageViewHeight = Math.min(parentHeight, (int) (parentWidth / imageAspectRatio));
|
||||
imageViewWidth = (int) (imageViewHeight * imageAspectRatio);
|
||||
}
|
||||
|
||||
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivBackground.getLayoutParams();
|
||||
layoutParams.width = imageViewWidth;
|
||||
layoutParams.height = imageViewHeight;
|
||||
ivBackground.setLayoutParams(layoutParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 带压缩的 Bitmap 解码(避免 OOM)
|
||||
*/
|
||||
private Bitmap decodeBitmapWithCompress(File file, int maxWidth, int maxHeight) {
|
||||
try {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
@@ -183,72 +334,41 @@ public class BackgroundView extends RelativeLayout {
|
||||
int scaleX = options.outWidth / maxWidth;
|
||||
int scaleY = options.outHeight / maxHeight;
|
||||
int inSampleSize = Math.max(scaleX, scaleY);
|
||||
if (inSampleSize <= 0) inSampleSize = 1;
|
||||
if (inSampleSize <= 0) {
|
||||
inSampleSize = 1;
|
||||
}
|
||||
|
||||
options.inJustDecodeBounds = false;
|
||||
options.inSampleSize = inSampleSize;
|
||||
options.inPreferredConfig = Bitmap.Config.RGB_565;
|
||||
return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "压缩解码失败:" + e.getMessage());
|
||||
LogUtils.e(TAG, "图片压缩加载失败:" + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调整ImageView尺寸(保持原图比例,在LinearLayout中居中平铺)
|
||||
* 设置默认背景(图片加载失败时兜底)
|
||||
*/
|
||||
private void adjustImageViewSize() {
|
||||
LogUtils.d(TAG, "=== adjustImageViewSize 启动 ===");
|
||||
if (mLlContainer == null || mIvBackground == null) {
|
||||
LogUtils.e(TAG, "控件为空");
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取LinearLayout尺寸
|
||||
int llWidth = mLlContainer.getWidth();
|
||||
int llHeight = mLlContainer.getHeight();
|
||||
if (llWidth == 0 || llHeight == 0) {
|
||||
postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
adjustImageViewSize();
|
||||
}
|
||||
}, 10);
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算ImageView尺寸(保持比例,不超出LinearLayout)
|
||||
int ivWidth, ivHeight;
|
||||
if (mImageAspectRatio >= 1.0f) {
|
||||
ivWidth = Math.min((int) (llHeight * mImageAspectRatio), llWidth);
|
||||
ivHeight = (int) (ivWidth / mImageAspectRatio);
|
||||
} else {
|
||||
ivHeight = Math.min((int) (llWidth / mImageAspectRatio), llHeight);
|
||||
ivWidth = (int) (ivHeight * mImageAspectRatio);
|
||||
}
|
||||
|
||||
// 应用尺寸
|
||||
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mIvBackground.getLayoutParams();
|
||||
params.width = ivWidth;
|
||||
params.height = ivHeight;
|
||||
mIvBackground.setLayoutParams(params);
|
||||
mIvBackground.setScaleType(ScaleType.FIT_CENTER); // 确保居中平铺
|
||||
|
||||
LogUtils.d(TAG, "ImageView尺寸:" + ivWidth + "x" + ivHeight);
|
||||
LogUtils.d(TAG, "=== adjustImageViewSize 完成 ===");
|
||||
private void setDefaultImageViewBackground() {
|
||||
ivBackground.setBackgroundResource(R.drawable.default_background);
|
||||
imageAspectRatio = 1.0f;
|
||||
adjustImageViewSize();
|
||||
LogUtils.d(TAG, "已设置 ImageView 默认背景");
|
||||
}
|
||||
|
||||
private void setDefaultTransparentBackground() {
|
||||
mIvBackground.setImageBitmap(null);
|
||||
mIvBackground.setBackgroundColor(0x00000000);
|
||||
mImageAspectRatio = 1.0f;
|
||||
}
|
||||
|
||||
// ====================================== 重写方法 ======================================
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
adjustImageViewSize(); // 尺寸变化时重新调整
|
||||
adjustImageViewSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对外提供:判断当前是否处于预览模式
|
||||
*/
|
||||
public boolean isPreviewMode() {
|
||||
return isPreviewMode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
31
powerbell/src/main/res/layout/activity_about.xml
Normal file
31
powerbell/src/main/res/layout/activity_about.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AToolbar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/toolbar_height"
|
||||
android:id="@+id/toolbar"
|
||||
android:gravity="center_vertical"
|
||||
style="@style/DefaultAToolbar"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1.0"
|
||||
android:id="@+id/root_ll"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<cc.winboll.studio.libaes.views.ASupportToolbar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/toolbar_height"
|
||||
android:id="@+id/toolbar"
|
||||
android:gravity="center_vertical"
|
||||
style="@style/DefaultAToolbar"/>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#FF28C000">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<cc.winboll.studio.powerbell.views.BackgroundView
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#FF3243E2"
|
||||
android:id="@+id/background_view">
|
||||
|
||||
</cc.winboll.studio.powerbell.views.BackgroundView>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="400dp"
|
||||
android:background="#B92FABE6">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="160dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="Origin BG"
|
||||
android:id="@+id/activitybackgroundpictureAButton5"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_margin="5dp"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="160dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="Received BG"
|
||||
android:id="@+id/activitybackgroundpictureAButton4"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_margin="5dp"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="right">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="◎"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton1"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="☑"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton2"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="♾"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton9"
|
||||
android:onClick="onNetworkBackgroundDialog"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="right">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[+]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton3"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[+~]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton6"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[◐]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton7"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[○]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton8"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
140
powerbell/src/main/res/layout/activity_backgroundpicture.xml
Normal file
140
powerbell/src/main/res/layout/activity_backgroundpicture.xml
Normal file
@@ -0,0 +1,140 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AToolbar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/toolbar_height"
|
||||
android:id="@+id/toolbar"
|
||||
style="@style/DefaultAToolbar"/>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/activitybackgroundpictureRelativeLayout1"/>
|
||||
|
||||
<cc.winboll.studio.powerbell.views.BackgroundView
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#FF7381FF"
|
||||
android:id="@+id/activitybackgroundpictureBackgroundView1">
|
||||
|
||||
</cc.winboll.studio.powerbell.views.BackgroundView>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/toolbar">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="160dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="Origin BG"
|
||||
android:id="@+id/activitybackgroundpictureAButton5"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_margin="5dp"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="160dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="Received BG"
|
||||
android:id="@+id/activitybackgroundpictureAButton4"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_margin="5dp"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="right">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="◎"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton1"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="☑"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton2"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="♾"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton9"
|
||||
android:onClick="onNetworkBackgroundDialog"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="right">
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[+]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton3"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[+~]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton6"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[◐]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton7"/>
|
||||
|
||||
<cc.winboll.studio.libaes.views.AButton
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="36dp"
|
||||
android:text="[○]"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_margin="5dp"
|
||||
android:id="@+id/activitybackgroundpictureAButton8"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
@@ -5,13 +5,6 @@
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:background="@android:color/white">
|
||||
|
||||
<cc.winboll.studio.libaes.views.ASupportToolbar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/toolbar_height"
|
||||
android:id="@+id/toolbar"
|
||||
android:gravity="center_vertical"
|
||||
style="@style/DefaultAToolbar"/>
|
||||
|
||||
<!-- 搜索框:提示文本改为“搜索应用名称或包名” -->
|
||||
<EditText
|
||||
|
||||
@@ -6,11 +6,10 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<cc.winboll.studio.libaes.views.ASupportToolbar
|
||||
<cc.winboll.studio.libaes.views.AToolbar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/toolbar_height"
|
||||
android:id="@+id/toolbar"
|
||||
android:gravity="center_vertical"
|
||||
style="@style/DefaultAToolbar"/>
|
||||
|
||||
<LinearLayout
|
||||
|
||||
@@ -21,232 +21,20 @@
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/activitymainRelativeLayout1"/>
|
||||
android:id="@+id/activitymainRelativeLayout1"
|
||||
android:background="#FFB7B7B7"/>
|
||||
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<cc.winboll.studio.powerbell.views.BackgroundView
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#FF7381FF"
|
||||
android:id="@+id/fragmentmainviewBackgroundView1"/>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentmainviewLinearLayout3"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:layout_marginTop="10dp">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:background="@drawable/bg_frame">
|
||||
|
||||
<Switch
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewSwitch1"
|
||||
android:padding="10dp"
|
||||
android:layout_weight="1.0"
|
||||
android:textSize="@dimen/text_title_size"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1.0"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1.0">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:id="@+id/fragmentmainviewLinearLayout1">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="36dp"
|
||||
android:layout_height="36dp"
|
||||
android:background="@drawable/usege"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"/>
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"
|
||||
android:id="@+id/fragmentmainviewCheckBox2"/>
|
||||
|
||||
<cc.winboll.studio.powerbell.views.VerticalSeekBar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewVerticalSeekBar2"
|
||||
android:progressTint="@color/colorUsege"
|
||||
android:progressBackgroundTint="@color/colorUsege"
|
||||
android:layout_weight="1.0"
|
||||
android:layout_margin="10dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100%"
|
||||
android:textSize="@dimen/text_title_size"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:id="@+id/fragmentandroidviewTextView3"
|
||||
android:gravity="center_horizontal"/>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewImageView2"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1.0">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100%"
|
||||
android:textSize="@dimen/text_title_size"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:gravity="center_horizontal"
|
||||
android:id="@+id/fragmentandroidviewTextView4"/>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewImageView1"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100%"
|
||||
android:textSize="@dimen/text_title_size"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:id="@+id/fragmentandroidviewTextView2"
|
||||
android:gravity="center_horizontal"/>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewImageView3"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:layout_marginTop="20dp"
|
||||
android:id="@+id/fragmentmainviewLinearLayout2">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="36dp"
|
||||
android:layout_height="36dp"
|
||||
android:background="@drawable/charge"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"/>
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"
|
||||
android:id="@+id/fragmentmainviewCheckBox1"/>
|
||||
|
||||
<cc.winboll.studio.powerbell.views.VerticalSeekBar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewVerticalSeekBar1"
|
||||
android:progressTint="@color/colorCharge"
|
||||
android:progressBackgroundTint="@color/colorCharge"
|
||||
android:layout_weight="1.0"
|
||||
android:layout_margin="10dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Tips"
|
||||
android:textSize="@dimen/text_content_size"
|
||||
android:id="@+id/fragmentandroidviewTextView1"
|
||||
android:background="@drawable/bg_frame"
|
||||
android:padding="10dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<cc.winboll.studio.libaes.views.ADsBannerView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/adsbanner"
|
||||
android:layout_alignParentBottom="true"/>
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/activitymainFrameLayout1"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<cc.winboll.studio.libaes.views.ADsBannerView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/adsbanner"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -5,47 +5,11 @@
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<RelativeLayout
|
||||
android:orientation="vertical"
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#FF0C6BBF">
|
||||
|
||||
<cc.winboll.studio.powerbell.views.BackgroundView
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/backgroundview"/>
|
||||
|
||||
<HorizontalScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#AF4FDA4E">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Main"
|
||||
android:id="@+id/btn_main_activity"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="TestCropImage"
|
||||
android:id="@+id/btn_test_cropimage"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</HorizontalScrollView>
|
||||
|
||||
</RelativeLayout>
|
||||
android:id="@+id/activitymainunittestFrameLayout1"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<cc.winboll.studio.libaes.views.ASupportToolbar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/toolbar_height"
|
||||
android:id="@+id/toolbar"
|
||||
android:gravity="center_vertical"
|
||||
style="@style/DefaultAToolbar"/>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="right">
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="CheckPermission"
|
||||
android:padding="10dp"
|
||||
android:onClick="onCheckPermission"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<cc.winboll.studio.libaes.views.ADsControlView
|
||||
android:id="@+id/ads_control_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
218
powerbell/src/main/res/layout/fragment_mainview.xml
Normal file
218
powerbell/src/main/res/layout/fragment_mainview.xml
Normal file
@@ -0,0 +1,218 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<cc.winboll.studio.powerbell.views.BackgroundView
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#FF7381FF"
|
||||
android:id="@+id/fragmentmainviewBackgroundView1"/>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentmainviewLinearLayout3"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:layout_marginTop="10dp">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:background="@drawable/bg_frame">
|
||||
|
||||
<Switch
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewSwitch1"
|
||||
android:padding="10dp"
|
||||
android:layout_weight="1.0"
|
||||
android:textSize="@dimen/text_title_size"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1.0"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1.0">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:id="@+id/fragmentmainviewLinearLayout1">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="36dp"
|
||||
android:layout_height="36dp"
|
||||
android:background="@drawable/usege"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"/>
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"
|
||||
android:id="@+id/fragmentmainviewCheckBox2"/>
|
||||
|
||||
<cc.winboll.studio.powerbell.views.VerticalSeekBar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewVerticalSeekBar2"
|
||||
android:progressTint="@color/colorUsege"
|
||||
android:progressBackgroundTint="@color/colorUsege"
|
||||
android:layout_weight="1.0"
|
||||
android:layout_margin="10dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100%"
|
||||
android:textSize="@dimen/text_title_size"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:id="@+id/fragmentandroidviewTextView3"
|
||||
android:gravity="center_horizontal"/>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewImageView2"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1.0">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100%"
|
||||
android:textSize="@dimen/text_title_size"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:gravity="center_horizontal"
|
||||
android:id="@+id/fragmentandroidviewTextView4"/>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewImageView1"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100%"
|
||||
android:textSize="@dimen/text_title_size"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:id="@+id/fragmentandroidviewTextView2"
|
||||
android:gravity="center_horizontal"/>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewImageView3"
|
||||
android:layout_weight="1.0"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:layout_marginTop="20dp"
|
||||
android:id="@+id/fragmentmainviewLinearLayout2">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="36dp"
|
||||
android:layout_height="36dp"
|
||||
android:background="@drawable/charge"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"/>
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="10dp"
|
||||
android:id="@+id/fragmentmainviewCheckBox1"/>
|
||||
|
||||
<cc.winboll.studio.powerbell.views.VerticalSeekBar
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fragmentandroidviewVerticalSeekBar1"
|
||||
android:progressTint="@color/colorCharge"
|
||||
android:progressBackgroundTint="@color/colorCharge"
|
||||
android:layout_weight="1.0"
|
||||
android:layout_margin="10dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Tips"
|
||||
android:textSize="@dimen/text_content_size"
|
||||
android:id="@+id/fragmentandroidviewTextView1"
|
||||
android:background="@drawable/bg_frame"
|
||||
android:padding="10dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<cc.winboll.studio.powerbell.views.BackgroundView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#FF7381FF">
|
||||
|
||||
<HorizontalScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Main"
|
||||
android:id="@+id/btn_main_activity"/>
|
||||
|
||||
</HorizontalScrollView>
|
||||
|
||||
</cc.winboll.studio.powerbell.views.BackgroundView>
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/bg_main">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/bg_imageview"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
@@ -9,10 +9,7 @@
|
||||
<item
|
||||
android:id="@+id/action_changepicture"
|
||||
android:title="@string/item_changepicture"/>
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:title="@string/item_settings"/>
|
||||
<item
|
||||
android:id="@+id/action_about"
|
||||
android:title="@string/item_about"/>
|
||||
android:title="@string/item_aboutview"/>
|
||||
</menu>
|
||||
|
||||
@@ -4,16 +4,13 @@
|
||||
<string name="app_name_cn2">泡额呗额</string>
|
||||
<string name="app_description">一个接收手机电量信息的应用,当电量值达到设定范围时会提醒用户。</string>
|
||||
<string name="about_crashed">本应用崩溃了,作者水平有限,敬请谅解!</string>
|
||||
<string name="item_main">主窗口</string>
|
||||
<string name="item_about">关于</string>
|
||||
<string name="item_settings">应用设置</string>
|
||||
<string name="item_battery_report">应用耗电记录</string>
|
||||
<string name="item_mainunittestactivity">开发调试窗口</string>
|
||||
<string name="item_clearrecord">清理电量记录</string>
|
||||
<string name="item_changepicture">更换背景图片</string>
|
||||
<string name="item_devoloperoptionsview">开发调试窗口</string>
|
||||
<string name="item_logview">日志窗口</string>
|
||||
<string name="item_sourceview">源码窗口</string>
|
||||
<string name="item_mainview">Main View</string>
|
||||
<string name="item_aboutview">About</string>
|
||||
<string name="item_clearrecord">Clear Record</string>
|
||||
<string name="item_changepicture">Change Picture</string>
|
||||
<string name="item_devoloperoptionsview">Developer View</string>
|
||||
<string name="item_logview">Log View</string>
|
||||
<string name="item_sourceview">Source View</string>
|
||||
<string name="txt_aboveswitch">消息总开关</string>
|
||||
<string name="txt_aboveswitchtips">当电量低于左边(放电状态)或高于右边(充电状态),就会发送一个提醒铃声。</string>
|
||||
<string name="texthint_CustomSlideToCleanRecord">Slide Right To Clean Up APP Record.</string>
|
||||
|
||||
@@ -9,12 +9,11 @@
|
||||
<string name="switchto_en1">PowerBell</string>
|
||||
<string name="switchto_cn1">能源钟</string>
|
||||
<string name="switchto_cn2">泡额呗额</string>
|
||||
<string name="en1_switch_disabled">PowerBell X</string>
|
||||
<string name="cn1_switch_disabled">能源钟 X</string>
|
||||
<string name="cn2_switch_disabled">泡额呗额 X</string>
|
||||
<string name="en1_switch_disabled">PowerBell</string>
|
||||
<string name="cn1_switch_disabled">能源钟</string>
|
||||
<string name="cn2_switch_disabled">泡额呗额</string>
|
||||
<string name="item_mainview">Main View</string>
|
||||
<string name="item_about">About</string>
|
||||
<string name="item_settings">Settings</string>
|
||||
<string name="item_aboutview">About</string>
|
||||
<string name="item_battery_report">Battery Report</string>
|
||||
<string name="item_clearrecord">Clear Record</string>
|
||||
<string name="item_changepicture">Change Picture</string>
|
||||
@@ -31,15 +30,4 @@
|
||||
<string name="subtitle_activity_pixelpicker">Pixel Picker</string>
|
||||
<string name="subtitle_activity_about">About The APP</string>
|
||||
<string name="msg_AOHPCTCSeekBar_ClearRecord">>>>Seek 100% Right Is Clean Record.>>></string>
|
||||
|
||||
<!-- 权限申请相关字符串(统一管理,避免硬编码) -->
|
||||
<string name="permission_title">权限申请</string>
|
||||
<string name="permission_denied_title">权限被拒绝</string>
|
||||
<string name="permission_grant_success">权限获取成功,请重新操作</string>
|
||||
<string name="permission_storage_rationale">需要存储权限才能选择/拍照/裁剪图片,请授予权限</string>
|
||||
<string name="permission_storage_setting_guide">存储权限已被拒绝且勾选“不再询问”,请前往设置页开启权限</string>
|
||||
<string name="confirm">确定</string>
|
||||
<string name="cancel">取消</string>
|
||||
<string name="go_to_setting">去设置</string>
|
||||
|
||||
</resources>
|
||||
|
||||
@@ -1,66 +1,28 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<!-- ====================================== 兼容适配:其他必要目录(可选)====================================== -->
|
||||
<!-- 应用内部缓存目录(适配少数依赖缓存的场景,如图片缓存)
|
||||
路径:/data/user/0/${applicationId}/cache/
|
||||
关联代码:getCacheDir() -->
|
||||
<paths>
|
||||
<external-path
|
||||
name="external_storage_root"
|
||||
path="." />
|
||||
<files-path
|
||||
name="files_path"
|
||||
path="." />
|
||||
<cache-path
|
||||
name="cache_path"
|
||||
path="." />
|
||||
|
||||
<!-- ====================================== 核心适配:应用私有外部目录(必选)====================================== -->
|
||||
<!-- 1. 裁剪临时文件目录(对应BackgroundSettingsActivity中onCreate的CropTemp目录)
|
||||
路径:/storage/emulated/0/Android/data/${applicationId}/files/Pictures/CropTemp/
|
||||
关联代码:_mSourceCropTempFile = new File(cropTempDir, _mSourceCropTempFileName) -->
|
||||
<external-files-path
|
||||
name="app_private_pictures"
|
||||
path="Pictures/" /> <!-- 仅保留1次,覆盖Pictures下所有子目录(含CropTemp) -->
|
||||
|
||||
<!-- 2. 背景图片目录(对应BackgroundSourceUtils的背景存储目录)
|
||||
路径:/storage/emulated/0/Android/data/${applicationId}/files/BackgroundPictureUtils/BackgroundSource/
|
||||
关联代码:mfBackgroundDir = new File(mBackgroundSourceUtils.getBackgroundSourceDirPath()) -->
|
||||
<external-files-path
|
||||
name="background_source"
|
||||
path="BackgroundPictureUtils/BackgroundSource/" />
|
||||
|
||||
<!-- 应用私有外部存储目录(适配BackgroundSourceUtils的目录) -->
|
||||
<!--/storage/emulated/0/Android/data/...-->
|
||||
<external-files-path
|
||||
name="external_file_path"
|
||||
path="BackgroundSourceUtils/" /> <!-- 对应fUtilsDir(BackgroundSourceUtils根目录) -->
|
||||
|
||||
<!-- 应用外部缓存目录(适配Android11+ 外部缓存场景,如第三方SDK依赖)
|
||||
路径:/storage/emulated/0/Android/data/${applicationId}/cache/
|
||||
关联代码:getExternalCacheDir() -->
|
||||
path="." />
|
||||
<external-files-path
|
||||
name="files_root"
|
||||
path="mimoDownload" />
|
||||
<!--代表app 外部存储区域根目录下的文件 Context.getExternalCacheDir目录下的目录-->
|
||||
<external-cache-path
|
||||
name="external_cache_path"
|
||||
path="." />
|
||||
|
||||
<!-- 3. 应用临时目录(对应App.getTempDirPath(),适配拍照临时文件)
|
||||
路径:/storage/emulated/0/Android/data/${applicationId}/files/temp/
|
||||
关联代码:mfPictureDir = new File(App.getTempDirPath())、mfTakePhoto = new File(mfPictureDir, "TakePhoto.jpg") -->
|
||||
<external-files-path
|
||||
name="app_temp"
|
||||
path="temp/" />
|
||||
|
||||
<!-- 通用应用外部文件目录(适配分享/下载等通用场景,覆盖files下所有目录)
|
||||
路径:/storage/emulated/0/Android/data/${applicationId}/files/
|
||||
关联代码:getExternalFilesDir(null) -->
|
||||
<external-files-path
|
||||
name="external_file_path"
|
||||
path="." />
|
||||
|
||||
<!-- 【核心添加】4. 应用内目录(getFilesDir(),对应别名 app_internal_files) -->
|
||||
<!-- 用于映射 /data/user/0/包名/files/ 路径,解决裁剪临时文件路径匹配问题 -->
|
||||
<files-path
|
||||
name="app_internal_files"
|
||||
path="." /> <!-- path="." 表示映射整个应用内目录 -->
|
||||
<!-- 关键新增:系统公共图片目录 /Pictures/PowerBell(图片存储/裁剪目录) -->
|
||||
<external-path
|
||||
name="public_pictures_powerbell"
|
||||
path="Pictures/PowerBell/" /> <!-- 路径:/storage/emulated/0/Pictures/PowerBell/ -->
|
||||
<!-- 兜底:应用内部缓存目录 -->
|
||||
<cache-path
|
||||
name="cache_path"
|
||||
path="BackgroundSourceUtils/" />
|
||||
<!--配置root-path。这样子可以读取到sd卡和一些应用分身的目录,否则微信分身保存的图片,就会导致 java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/999/tencent/MicroMsg/WeiXin/export1544062754693.jpg,在小米6的手机上微信分身有这个crash,华为没有
|
||||
-->
|
||||
<root-path
|
||||
name="root_path"
|
||||
path="" />
|
||||
</paths>
|
||||
|
||||
Reference in New Issue
Block a user