refactor(settings): 替换自定义 ToggleButton 为 SwitchButton 并优化默认 launcher 设置逻辑
This commit is contained in:
@@ -296,7 +296,6 @@ dependencies {
|
||||
// implementation 'io.github.jeremyliao:live-event-bus:1.8.0'
|
||||
implementation 'com.github.neo-turak:LiveEventBus:1.8.1'
|
||||
|
||||
implementation 'com.facebook.rebound:rebound:0.3.8'
|
||||
//MMKV
|
||||
implementation 'com.tencent:mmkv-static:2.4.0'
|
||||
//bugly
|
||||
|
||||
@@ -5,8 +5,8 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.view.View;
|
||||
|
||||
import com.suke.widget.SwitchButton;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
import com.ttstd.dialer.BuildConfig;
|
||||
import com.ttstd.dialer.R;
|
||||
import com.ttstd.dialer.activity.main.MainActivity;
|
||||
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
|
||||
@@ -17,7 +17,6 @@ import com.ttstd.dialer.service.main.MainService;
|
||||
import com.ttstd.dialer.utils.CameraUtil;
|
||||
import com.ttstd.dialer.utils.Logger;
|
||||
import com.ttstd.dialer.utils.SystemUtils;
|
||||
import com.ttstd.dialer.view.ToggleButton;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
@@ -62,11 +61,11 @@ public class SettingsUtilsActivity extends BaseMvvmActivity<SettingsUtilsViewMod
|
||||
mViewDataBinding.siFloatWindowKill.setEnabled(false);
|
||||
}
|
||||
|
||||
mViewDataBinding.siFloatWindow.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
|
||||
mViewDataBinding.siFloatWindow.setOnToggleChanged(new SwitchButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onToggle(boolean on) {
|
||||
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
|
||||
Intent intent = new Intent(SettingsUtilsActivity.this, MainService.class);
|
||||
if (on) {
|
||||
if (isChecked) {
|
||||
mViewDataBinding.siFloatWindowKill.setEnabled(true);
|
||||
intent.setAction(MainService.SHOW_FLOAT_WINDOW_ACTION);
|
||||
mMMKV.encode(CommonConfig.FLOAT_WINDOW_ENABLE, 1);
|
||||
@@ -80,10 +79,10 @@ public class SettingsUtilsActivity extends BaseMvvmActivity<SettingsUtilsViewMod
|
||||
});
|
||||
|
||||
|
||||
mViewDataBinding.siFloatWindowKill.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
|
||||
mViewDataBinding.siFloatWindowKill.setOnToggleChanged(new SwitchButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onToggle(boolean on) {
|
||||
if (on) {
|
||||
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
mMMKV.encode(CommonConfig.FLOAT_WINDOW_KILL_APP, 1);
|
||||
} else {
|
||||
mMMKV.encode(CommonConfig.FLOAT_WINDOW_KILL_APP, 0);
|
||||
@@ -91,23 +90,21 @@ public class SettingsUtilsActivity extends BaseMvvmActivity<SettingsUtilsViewMod
|
||||
}
|
||||
});
|
||||
|
||||
mViewDataBinding.siDefaultLauncher.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
|
||||
mViewDataBinding.siDefaultLauncher.setOnToggleChanged(new SwitchButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onToggle(boolean on) {
|
||||
if (on) {
|
||||
// SystemUtils.setDefaultLauncher(SettingsUtilsActivity.this, MainActivity.class);
|
||||
// SystemUtils.setDefaultLauncher(SettingsUtilsActivity.this);
|
||||
SystemUtils.addRoleHolderAsUser(SettingsUtilsActivity.this, BuildConfig.APPLICATION_ID);
|
||||
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
SystemUtils.setDefaultLauncher(SettingsUtilsActivity.this, MainActivity.class);
|
||||
} else {
|
||||
SystemUtils.setOtherDefaultLauncher(SettingsUtilsActivity.this);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mViewDataBinding.siHourlyChime.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
|
||||
mViewDataBinding.siHourlyChime.setOnToggleChanged(new SwitchButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onToggle(boolean on) {
|
||||
if (on) {
|
||||
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
mMMKV.encode(CommonConfig.HOURLY_CHIME_ENABLE, 1);
|
||||
com.ttstd.dialer.receiver.HourlyChimeManager.startChime(SettingsUtilsActivity.this);
|
||||
} else {
|
||||
|
||||
@@ -170,8 +170,8 @@ public class ApkUtils {
|
||||
}
|
||||
// 是系统中已安装的应用
|
||||
if (pi != null) {
|
||||
boolean isSysApp = (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1;
|
||||
boolean isSysUpd = (pi.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1;
|
||||
boolean isSysApp = (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
|
||||
boolean isSysUpd = (pi.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
|
||||
isSystemApp = isSysApp || isSysUpd;
|
||||
}
|
||||
return isSystemApp;
|
||||
|
||||
@@ -35,8 +35,6 @@ import android.util.Log;
|
||||
import android.view.SurfaceControl;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.ttstd.dialer.BuildConfig;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
@@ -484,47 +482,44 @@ public class SystemUtils {
|
||||
* @param launcherActivityClass 你的桌面 Activity 类,例如 MyLauncherActivity.class
|
||||
*/
|
||||
public static void setDefaultLauncher(Context context, Class<?> launcherActivityClass) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
addRoleHolderAsUser(context, context.getPackageName());
|
||||
} else {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
|
||||
// 1. 创建桌面过滤的 IntentFilter
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
// 1. 创建桌面过滤的 IntentFilter
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
|
||||
// 2. 查询当前系统中所有的桌面应用(用来构建 ComponentName 数组)
|
||||
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
||||
homeIntent.addCategory(Intent.CATEGORY_HOME);
|
||||
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
// 2. 查询当前系统中所有的桌面应用(用来构建 ComponentName 数组)
|
||||
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
||||
homeIntent.addCategory(Intent.CATEGORY_HOME);
|
||||
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
|
||||
int bestMatch = 0;
|
||||
ComponentName[] set = new ComponentName[resolveInfos.size()];
|
||||
for (int i = 0; i < resolveInfos.size(); i++) {
|
||||
ResolveInfo info = resolveInfos.get(i);
|
||||
set[i] = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
||||
if (info.match > bestMatch) {
|
||||
bestMatch = info.match; // 获取最匹配的值
|
||||
int bestMatch = 0;
|
||||
ComponentName[] set = new ComponentName[resolveInfos.size()];
|
||||
for (int i = 0; i < resolveInfos.size(); i++) {
|
||||
ResolveInfo info = resolveInfos.get(i);
|
||||
set[i] = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
||||
if (info.match > bestMatch) {
|
||||
bestMatch = info.match; // 获取最匹配的值
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 构建你自己的桌面 ComponentName
|
||||
ComponentName myLauncher = new ComponentName(context, launcherActivityClass);
|
||||
// 3. 构建你自己的桌面 ComponentName
|
||||
ComponentName myLauncher = new ComponentName(context, launcherActivityClass);
|
||||
|
||||
// 4. 替换首选 Activity(核心步骤)
|
||||
try {
|
||||
// 注意:API 29 (Android 10) 中此方法对第三方应用废弃,但对系统签名应用依然有效
|
||||
pm.replacePreferredActivity(filter, bestMatch, set, myLauncher);
|
||||
Logger.i("LauncherHelper", "成功设置为默认桌面");
|
||||
|
||||
// 可选:发送一个回到桌面的 Intent 来验证
|
||||
// Intent startHome = new Intent(Intent.ACTION_MAIN);
|
||||
// startHome.addCategory(Intent.CATEGORY_HOME);
|
||||
// startHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
// context.startActivity(startHome);
|
||||
|
||||
} catch (SecurityException e) {
|
||||
Logger.e("LauncherHelper", "缺少权限或未生效,请检查系统签名是否正确", e);
|
||||
} catch (Exception e) {
|
||||
Logger.e("LauncherHelper", "设置默认桌面失败", e);
|
||||
// 4. 替换首选 Activity(核心步骤)
|
||||
try {
|
||||
// 注意:API 29 (Android 10) 中此方法对第三方应用废弃,但对系统签名应用依然有效
|
||||
pm.replacePreferredActivity(filter, bestMatch, set, myLauncher);
|
||||
Logger.i("LauncherHelper", "成功设置为默认桌面");
|
||||
} catch (SecurityException e) {
|
||||
Logger.e("LauncherHelper", "缺少权限或未生效,请检查系统签名是否正确", e);
|
||||
} catch (Exception e) {
|
||||
Logger.e("LauncherHelper", "设置默认桌面失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,25 +602,39 @@ public class SystemUtils {
|
||||
public static void setOtherDefaultLauncher(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
|
||||
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
||||
homeIntent.addCategory(Intent.CATEGORY_HOME);
|
||||
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
Optional<ResolveInfo> systemLauncher = resolveInfos.stream().filter(new Predicate<ResolveInfo>() {
|
||||
@Override
|
||||
public boolean test(ResolveInfo resolveInfo) {
|
||||
return !BuildConfig.APPLICATION_ID.equals(resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
}).filter(new Predicate<ResolveInfo>() {
|
||||
@Override
|
||||
public boolean test(ResolveInfo resolveInfo) {
|
||||
return ApkUtils.isSystemApp(context, resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
}).findAny();
|
||||
systemLauncher.ifPresent(resolveInfo -> addRoleHolderAsUser(context, resolveInfo.activityInfo.packageName));
|
||||
|
||||
Optional<ResolveInfo> systemLauncher = resolveInfos.stream()
|
||||
.filter(info -> !context.getPackageName().equals(info.activityInfo.packageName))
|
||||
.filter(info -> ApkUtils.isSystemApp(context, info.activityInfo.packageName))
|
||||
.findFirst();
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
systemLauncher.ifPresent(info -> addRoleHolderAsUser(context, info.activityInfo.packageName));
|
||||
} else {
|
||||
pm.clearPackagePreferredActivities(context.getPackageName());
|
||||
systemLauncher.ifPresent(info -> {
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
|
||||
int bestMatch = 0;
|
||||
ComponentName[] set = new ComponentName[resolveInfos.size()];
|
||||
for (int i = 0; i < resolveInfos.size(); i++) {
|
||||
ResolveInfo res = resolveInfos.get(i);
|
||||
set[i] = new ComponentName(res.activityInfo.packageName, res.activityInfo.name);
|
||||
if (res.match > bestMatch) bestMatch = res.match;
|
||||
}
|
||||
ComponentName otherLauncher = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
||||
try {
|
||||
pm.replacePreferredActivity(filter, bestMatch, set, otherLauncher);
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "setOtherDefaultLauncher error", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
|
||||
import com.suke.widget.SwitchButton;
|
||||
import com.ttstd.dialer.R;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -21,7 +22,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class SettingItem extends ConstraintLayout {
|
||||
|
||||
private OnClickListener mRootOnClickListener;
|
||||
private ToggleButton.OnToggleChanged mOnToggleChanged;
|
||||
private SwitchButton.OnCheckedChangeListener mOnToggleChanged;
|
||||
|
||||
private static final String DefaultOptionsText = "设置选项";
|
||||
private static final String DefaultEnableText = "开启描述";
|
||||
@@ -42,7 +43,7 @@ public class SettingItem extends ConstraintLayout {
|
||||
|
||||
private ConstraintLayout cl_root;
|
||||
private TextView tv_options, tv_hint;
|
||||
private ToggleButton tb;
|
||||
private SwitchButton switch_button;
|
||||
private ImageView iv_more;
|
||||
private View dividerLine;
|
||||
|
||||
@@ -50,13 +51,13 @@ public class SettingItem extends ConstraintLayout {
|
||||
mRootOnClickListener = rootOnClickListener;
|
||||
}
|
||||
|
||||
public void setOnToggleChanged(ToggleButton.OnToggleChanged onToggleChanged) {
|
||||
public void setOnToggleChanged(SwitchButton.OnCheckedChangeListener onToggleChanged) {
|
||||
mOnToggleChanged = onToggleChanged;
|
||||
tb.setOnToggleChanged(mOnToggleChanged);
|
||||
switch_button.setOnCheckedChangeListener(mOnToggleChanged);
|
||||
}
|
||||
|
||||
public void setToggleStatu(boolean on) {
|
||||
tb.setToggleStatu(on);
|
||||
switch_button.setChecked(on);
|
||||
if (on) {
|
||||
if (!TextUtils.isEmpty(mEnableText)) {
|
||||
tv_hint.setText(mEnableText);
|
||||
@@ -74,7 +75,7 @@ public class SettingItem extends ConstraintLayout {
|
||||
}
|
||||
|
||||
public boolean isChecked() {
|
||||
return tb.isToggleOn();
|
||||
return switch_button.isChecked();
|
||||
}
|
||||
|
||||
public SettingItem(@NonNull @NotNull Context context) {
|
||||
@@ -104,7 +105,7 @@ public class SettingItem extends ConstraintLayout {
|
||||
cl_root = findViewById(R.id.cl_root);
|
||||
tv_options = findViewById(R.id.tv_options);
|
||||
tv_hint = findViewById(R.id.tv_hint);
|
||||
tb = findViewById(R.id.tb);
|
||||
switch_button = findViewById(R.id.switch_button);
|
||||
iv_more = findViewById(R.id.iv_more);
|
||||
dividerLine = findViewById(R.id.divider);
|
||||
|
||||
@@ -153,9 +154,9 @@ public class SettingItem extends ConstraintLayout {
|
||||
tv_hint.setVisibility(GONE);
|
||||
}
|
||||
if (mShowToggle) {
|
||||
tb.setVisibility(VISIBLE);
|
||||
switch_button.setVisibility(VISIBLE);
|
||||
} else {
|
||||
tb.setVisibility(GONE);
|
||||
switch_button.setVisibility(GONE);
|
||||
}
|
||||
if (mShowMore) {
|
||||
iv_more.setVisibility(VISIBLE);
|
||||
@@ -168,7 +169,7 @@ public class SettingItem extends ConstraintLayout {
|
||||
dividerLine.setVisibility(GONE);
|
||||
}
|
||||
|
||||
if (tb.isToggleOn()) {
|
||||
if (switch_button.isChecked()) {
|
||||
if (!TextUtils.isEmpty(mEnableText)) {
|
||||
tv_hint.setText(mEnableText);
|
||||
} else {
|
||||
@@ -181,10 +182,10 @@ public class SettingItem extends ConstraintLayout {
|
||||
tv_hint.setText(DefaultDisableText);
|
||||
}
|
||||
}
|
||||
tb.setOnToggleInsideChanged(new ToggleButton.OnToggleInsideChanged() {
|
||||
switch_button.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onToggle(boolean on) {
|
||||
if (on) {
|
||||
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
if (!TextUtils.isEmpty(mEnableText)) {
|
||||
tv_hint.setText(mEnableText);
|
||||
} else {
|
||||
@@ -207,10 +208,10 @@ public class SettingItem extends ConstraintLayout {
|
||||
cl_root.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (tb.isToggleOn()) {
|
||||
tb.toggleOff();
|
||||
if (switch_button.isChecked()) {
|
||||
switch_button.setChecked(false);
|
||||
} else {
|
||||
tb.toggleOn();
|
||||
switch_button.setChecked(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -221,7 +222,7 @@ public class SettingItem extends ConstraintLayout {
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
super.setEnabled(enabled);
|
||||
tb.setEnabled(enabled);
|
||||
switch_button.setEnabled(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,389 +0,0 @@
|
||||
package com.ttstd.dialer.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.RectF;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
import android.view.View;
|
||||
|
||||
import com.facebook.rebound.SimpleSpringListener;
|
||||
import com.facebook.rebound.Spring;
|
||||
import com.facebook.rebound.SpringConfig;
|
||||
import com.facebook.rebound.SpringSystem;
|
||||
import com.facebook.rebound.SpringUtil;
|
||||
import com.ttstd.dialer.R;
|
||||
import com.ttstd.dialer.utils.Logger;
|
||||
|
||||
public class ToggleButton extends View {
|
||||
private static final String TAG = "ToggleButton";
|
||||
private SpringSystem springSystem;
|
||||
private Spring spring;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private float radius;
|
||||
/**
|
||||
* 开启颜色
|
||||
*/
|
||||
private int onColor = Color.parseColor("#246FFE");
|
||||
/**
|
||||
* 关闭颜色
|
||||
*/
|
||||
private int offBorderColor = Color.parseColor("#c7c7c7");
|
||||
/**
|
||||
* 灰色带颜色
|
||||
*/
|
||||
private int offColor = Color.parseColor("#ffffff");
|
||||
/**
|
||||
* 手柄颜色
|
||||
*/
|
||||
private int spotColor = Color.parseColor("#ffffff");
|
||||
/**
|
||||
* 边框颜色
|
||||
*/
|
||||
private int borderColor = offBorderColor;
|
||||
/**
|
||||
* 画笔
|
||||
*/
|
||||
private Paint paint;
|
||||
/**
|
||||
* 开关状态
|
||||
*/
|
||||
private boolean toggleOn = false;
|
||||
/**
|
||||
* 边框大小
|
||||
*/
|
||||
private int borderWidth = 2;
|
||||
/**
|
||||
* 垂直中心
|
||||
*/
|
||||
private float centerY;
|
||||
/**
|
||||
* 按钮的开始和结束位置
|
||||
*/
|
||||
private float startX, endX;
|
||||
/**
|
||||
* 手柄X位置的最小和最大值
|
||||
*/
|
||||
private float spotMinX, spotMaxX;
|
||||
/**
|
||||
* 手柄大小
|
||||
*/
|
||||
private int spotSize;
|
||||
/**
|
||||
* 手柄X位置
|
||||
*/
|
||||
private float spotX;
|
||||
/**
|
||||
* 关闭时内部灰色带高度
|
||||
*/
|
||||
private float offLineWidth;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private RectF rect = new RectF();
|
||||
/**
|
||||
* 默认使用动画
|
||||
*/
|
||||
private boolean defaultAnimate = true;
|
||||
/**
|
||||
* 是否默认处于打开状态
|
||||
*/
|
||||
private boolean isDefaultOn = false;
|
||||
/**
|
||||
* 禁止点击
|
||||
*/
|
||||
private boolean disable = false;
|
||||
|
||||
private OnToggleChanged listener;
|
||||
|
||||
public void setDisable(boolean dis) {
|
||||
this.disable = dis;
|
||||
}
|
||||
|
||||
private ToggleButton(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
setup(attrs);
|
||||
}
|
||||
|
||||
public ToggleButton(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setup(attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
spring.removeListener(springListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
spring.addListener(springListener);
|
||||
}
|
||||
|
||||
public void setup(AttributeSet attrs) {
|
||||
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
paint.setStyle(Paint.Style.FILL);
|
||||
paint.setStrokeCap(Paint.Cap.ROUND);
|
||||
springSystem = SpringSystem.create();
|
||||
spring = springSystem.createSpring();
|
||||
//张力(tension),摩擦力(friction)
|
||||
//增大张力会使弹簧更快地向目标值运动,减小摩擦力会减少弹簧运动过程中的阻力,从而使回弹更加迅速和有力。
|
||||
spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(80, 10));
|
||||
this.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View arg0) {
|
||||
if (disable) {
|
||||
|
||||
} else {
|
||||
toggle(defaultAnimate);
|
||||
}
|
||||
}
|
||||
});
|
||||
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ToggleButton);
|
||||
offBorderColor = typedArray.getColor(R.styleable.ToggleButton_tbOffBorderColor, offBorderColor);
|
||||
onColor = typedArray.getColor(R.styleable.ToggleButton_tbOnColor, onColor);
|
||||
spotColor = typedArray.getColor(R.styleable.ToggleButton_tbSpotColor, spotColor);
|
||||
offColor = typedArray.getColor(R.styleable.ToggleButton_tbOffColor, offColor);
|
||||
borderWidth = typedArray.getDimensionPixelSize(R.styleable.ToggleButton_tbBorderWidth, borderWidth);
|
||||
defaultAnimate = typedArray.getBoolean(R.styleable.ToggleButton_tbAnimate, defaultAnimate);
|
||||
isDefaultOn = typedArray.getBoolean(R.styleable.ToggleButton_tbAsDefaultOn, isDefaultOn);
|
||||
typedArray.recycle();
|
||||
borderColor = offBorderColor;
|
||||
if (isDefaultOn) {
|
||||
toggleOn();
|
||||
}
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
toggle(true);
|
||||
}
|
||||
|
||||
public void toggle(boolean animate) {
|
||||
toggleOn = !toggleOn;
|
||||
Logger.e(TAG, "toggle: toggleOn = " + toggleOn);
|
||||
takeEffect(animate);
|
||||
if (listener != null) {
|
||||
listener.onToggle(toggleOn);
|
||||
}
|
||||
if (mOnToggleInsideChanged != null) {
|
||||
mOnToggleInsideChanged.onToggle(toggleOn);
|
||||
}
|
||||
}
|
||||
|
||||
public void toggleOn() {
|
||||
setToggleOn();
|
||||
if (listener != null) {
|
||||
listener.onToggle(toggleOn);
|
||||
}
|
||||
if (mOnToggleInsideChanged != null) {
|
||||
mOnToggleInsideChanged.onToggle(toggleOn);
|
||||
}
|
||||
}
|
||||
|
||||
public void toggleOff() {
|
||||
setToggleOff();
|
||||
if (listener != null) {
|
||||
listener.onToggle(toggleOn);
|
||||
}
|
||||
if (mOnToggleInsideChanged != null) {
|
||||
mOnToggleInsideChanged.onToggle(toggleOn);
|
||||
}
|
||||
}
|
||||
|
||||
public void setToggleStatu(boolean on) {
|
||||
if (on) {
|
||||
setToggleOn();
|
||||
} else {
|
||||
setToggleOff();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置显示成打开样式,不会触发toggle事件
|
||||
*/
|
||||
public void setToggleOn() {
|
||||
setToggleOn(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param animate asd
|
||||
*/
|
||||
public void setToggleOn(boolean animate) {
|
||||
toggleOn = true;
|
||||
takeEffect(animate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置显示成关闭样式,不会触发toggle事件
|
||||
*/
|
||||
public void setToggleOff() {
|
||||
setToggleOff(true);
|
||||
}
|
||||
|
||||
public void setToggleOff(boolean animate) {
|
||||
toggleOn = false;
|
||||
takeEffect(animate);
|
||||
}
|
||||
|
||||
|
||||
public int getToggleOnStatu() {
|
||||
Logger.e(TAG, "getToggleOnStatu: " + toggleOn);
|
||||
return toggleOn ? 1 : 0;
|
||||
}
|
||||
|
||||
public boolean isToggleOn() {
|
||||
Logger.e(TAG, "isToggleOn: " + toggleOn);
|
||||
return toggleOn;
|
||||
}
|
||||
|
||||
private void takeEffect(boolean animate) {
|
||||
if (animate) {
|
||||
spring.setEndValue(toggleOn ? 1 : 0);
|
||||
} else {
|
||||
//这里没有调用spring,所以spring里的当前值没有变更,这里要设置一下,同步两边的当前值
|
||||
spring.setCurrentValue(toggleOn ? 1 : 0);
|
||||
calculateEffect(toggleOn ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
|
||||
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
|
||||
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
|
||||
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
|
||||
Resources r = Resources.getSystem();
|
||||
if (widthMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.AT_MOST) {
|
||||
widthSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics());
|
||||
widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
|
||||
}
|
||||
if (heightMode == MeasureSpec.UNSPECIFIED || heightSize == MeasureSpec.AT_MOST) {
|
||||
heightSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, r.getDisplayMetrics());
|
||||
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
|
||||
}
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int left, int top, int right,
|
||||
int bottom) {
|
||||
super.onLayout(changed, left, top, right, bottom);
|
||||
final int width = getWidth();
|
||||
final int height = getHeight();
|
||||
radius = Math.min(width, height) * 0.5f;
|
||||
centerY = radius;
|
||||
startX = radius;
|
||||
endX = width - radius;
|
||||
spotMinX = startX + borderWidth;
|
||||
spotMaxX = endX - borderWidth;
|
||||
spotSize = height - 4 * borderWidth;
|
||||
spotX = toggleOn ? spotMaxX : spotMinX;
|
||||
offLineWidth = 0;
|
||||
}
|
||||
|
||||
SimpleSpringListener springListener = new SimpleSpringListener() {
|
||||
@Override
|
||||
public void onSpringUpdate(Spring spring) {
|
||||
final double value = spring.getCurrentValue();
|
||||
calculateEffect(value);
|
||||
}
|
||||
};
|
||||
|
||||
private int clamp(int value, int low, int high) {
|
||||
return Math.min(Math.max(value, low), high);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
//
|
||||
super.draw(canvas);
|
||||
rect.set(0, 0, getWidth(), getHeight());
|
||||
paint.setColor(borderColor);
|
||||
canvas.drawRoundRect(rect, radius, radius, paint);
|
||||
if (offLineWidth > 0) {
|
||||
final float cy = offLineWidth * 0.5f;
|
||||
rect.set(spotX - cy, centerY - cy, endX + cy, centerY + cy);
|
||||
// paint.setColor(offColor);
|
||||
canvas.drawRoundRect(rect, cy, cy, paint);
|
||||
}
|
||||
rect.set(spotX - 1 - radius, centerY - radius, spotX + 1.1f + radius, centerY + radius);
|
||||
paint.setColor(borderColor);
|
||||
canvas.drawRoundRect(rect, radius, radius, paint);
|
||||
final float spotR = spotSize * 0.5f;
|
||||
rect.set(spotX - spotR, centerY - spotR, spotX + spotR, centerY + spotR);
|
||||
paint.setColor(spotColor);
|
||||
canvas.drawRoundRect(rect, spotR, spotR, paint);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value
|
||||
*/
|
||||
private void calculateEffect(final double value) {
|
||||
final float mapToggleX = (float) SpringUtil.mapValueFromRangeToRange(value, 0, 1, spotMinX, spotMaxX);
|
||||
spotX = mapToggleX;
|
||||
float mapOffLineWidth = (float) SpringUtil.mapValueFromRangeToRange(1 - value, 0, 1, 10, spotSize);
|
||||
offLineWidth = mapOffLineWidth;
|
||||
final int fb = Color.blue(onColor);
|
||||
final int fr = Color.red(onColor);
|
||||
final int fg = Color.green(onColor);
|
||||
final int tb = Color.blue(offBorderColor);
|
||||
final int tr = Color.red(offBorderColor);
|
||||
final int tg = Color.green(offBorderColor);
|
||||
int sb = (int) SpringUtil.mapValueFromRangeToRange(1 - value, 0, 1, fb, tb);
|
||||
int sr = (int) SpringUtil.mapValueFromRangeToRange(1 - value, 0, 1, fr, tr);
|
||||
int sg = (int) SpringUtil.mapValueFromRangeToRange(1 - value, 0, 1, fg, tg);
|
||||
sb = clamp(sb, 0, 255);
|
||||
sr = clamp(sr, 0, 255);
|
||||
sg = clamp(sg, 0, 255);
|
||||
borderColor = Color.rgb(sr, sg, sb);
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @author ThinkPad
|
||||
*/
|
||||
public interface OnToggleChanged {
|
||||
/**
|
||||
* @param on = =
|
||||
*/
|
||||
public void onToggle(boolean on);
|
||||
}
|
||||
|
||||
public interface OnToggleInsideChanged {
|
||||
/**
|
||||
* @param on = =
|
||||
*/
|
||||
void onToggle(boolean on);
|
||||
}
|
||||
|
||||
private OnToggleInsideChanged mOnToggleInsideChanged;
|
||||
|
||||
public void setOnToggleInsideChanged(OnToggleInsideChanged onToggleChanged) {
|
||||
mOnToggleInsideChanged = onToggleChanged;
|
||||
}
|
||||
|
||||
public void setOnToggleChanged(OnToggleChanged onToggleChanged) {
|
||||
listener = onToggleChanged;
|
||||
}
|
||||
|
||||
public boolean isAnimate() {
|
||||
return defaultAnimate;
|
||||
}
|
||||
|
||||
public void setAnimate(boolean animate) {
|
||||
this.defaultAnimate = animate;
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/tb"
|
||||
app:layout_constraintEnd_toStartOf="@+id/switch_button"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
@@ -49,11 +49,13 @@
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<com.ttstd.dialer.view.ToggleButton
|
||||
android:id="@+id/tb"
|
||||
android:layout_width="@dimen/dp_48"
|
||||
android:layout_height="@dimen/dp_24"
|
||||
android:layout_marginEnd="16dp"
|
||||
<com.suke.widget.SwitchButton
|
||||
android:id="@+id/switch_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
app:sb_show_indicator="false"
|
||||
app:sb_checked="false"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/iv_more"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
@@ -1,15 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<declare-styleable name="ToggleButton">
|
||||
<attr name="tbBorderWidth" format="dimension" />
|
||||
<attr name="tbOffBorderColor" format="reference|color" />
|
||||
<attr name="tbOffColor" format="reference|color" />
|
||||
<attr name="tbOnColor" format="reference|color" />
|
||||
<attr name="tbSpotColor" format="reference|color" />
|
||||
<attr name="tbAnimate" format="reference|boolean" />
|
||||
<attr name="tbAsDefaultOn" format="reference|boolean" />
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="SettingItem">
|
||||
<attr name="optionsText" format="reference|string" />
|
||||
<attr name="optionsTextColor" format="reference|color" />
|
||||
|
||||
Reference in New Issue
Block a user