在系统中设置添加一个控制扫码使能开关,在其他地方可以获取这个值,并根据这个值做相关处理。
1.添加自定义属性变量名
修改: /frameworks/base/core/java/android/provider/Settings.java
public static final class System extends NameValueTable {
...
1. 添加变量名称
/**
* add by wxd for qrscan
*/
public static final String QRSCAN_MODE = "qrscan_mode";
private static final Validator QRSCAN_MODE_VALIDATOR = BOOLEAN_VALIDATOR; // 不加这个编译不过
...
2. SETTINGS_TO_BACKUP中添加
public static final String[] SETTINGS_TO_BACKUP = {
QRSCAN_MODE,
STAY_ON_WHILE_PLUGGED_IN, // moved to global
...
3. PUBLIC_SETTINGS中添加
public static final Set<String> PUBLIC_SETTINGS = new ArraySet<>();
static {
PUBLIC_SETTINGS.add(QRSCAN_MODE);
PUBLIC_SETTINGS.add(END_BUTTON_BEHAVIOR);
...
4. VALIDATORS中添加
public static final Map<String, Validator> VALIDATORS = new ArrayMap<>();
static {
VALIDATORS.put(QRSCAN_MODE, QRSCAN_MODE_VALIDATOR);
...
2.添加新文件QrScanPreferenceController,改变值并发出通知
添加: vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/security/QrScanPreferenceController.java
package com.android.settings.security;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.R;
public class QrScanPreferenceController extends TogglePreferenceController {
private Context mContext;
private static final String KEY_QRSCAN_SETTING = "qrscan_setting";
private static final String QRSCAN_SETTING_ENABLE = "key_qrscan_setting";
private int QRSCAN_ON = 1; // 打开
private int QRSCAN_OFF = 0; // 关闭
public QrScanPreferenceController(Context context) {
super(context, KEY_QRSCAN_SETTING);
mContext = context;
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
Settings.System.QRSCAN_MODE, QRSCAN_ON ) != QRSCAN_ON ;
}
@Override
public boolean setChecked(boolean isChecked) {
Settings.System.putInt(mContext.getContentResolver(), Settings.System.QRSCAN_MODE,
isChecked ? QRSCAN_ON : QRSCAN_ON );
mContext.getContentResolver().notifyChange(Settings.System.getUriFor(
Settings.System.QRSCAN_MODE),null); // 发出通知QRSCAN_MODE的值发生变化
return true;
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE; // 其他值不会显示
}
}
3.加入到Setting的布局中
添加: /vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/security_dashboard_settings.xml
<SwitchPreference
android:key="qrscan_setting"
android:title="@string/qrscan_setting"
android:summary="@string/qrscan_setting_summery"
settings:controller="com.android.settings.security.QrScanPreferenceController"/>
添加: /vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/security/SecuritySettings.java
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle, SecuritySettings host) {
...
controllers.add(new QrScanPreferenceController(context));
return controllers;
}
3.注册ContentObserver,值发生变化时得到通知
添加: /frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
class SettingsObserver extends ContentObserver {
SettingsObserver(Handler handler) {
super(handler);
}
void observe() {
// Observe all users' changes
ContentResolver resolver = mContext.getContentResolver();
...
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.QRSCAN_MODE), false, this,
UserHandle.USER_ALL); // add by wxd
updateSettings();
}
@Override
public void onChange(boolean selfChange) { // 值发生变化
int qrscanMode = Settings.System.getInt(mContext.getContentResolver(),Settings.System.QRSCAN_MODE, 1);
}
}