代码为公司的定制版基于android5.1,没有具体比较锁屏部分应该和原生代码有所不同。本文基于浏览代码结果,没有手机版本和代码版本不一致(APP上还是一脉相承)所以没有在手机上直接试验。
首先在自己手机的版本上显示的默认锁屏方式在“设置”->“安全”->“屏幕锁定方式”其值为“滑动”,
首先寻找“滑动”字符串packages/apps/Settings/res/values-zh-rCN/strings.xml
<string name="unlock_set_unlock_none_title" msgid="5679243878975864640">"滑动"</string>
被引用:
packages/apps/Settings/res/xml/security_settings_picker.xml
被引用:
packages/apps/Settings/src/com/android/settings/ChooseLockGeneric.java
没有其他发现,转而向寻找“安全”
<string name="security_settings_title" msgid="7945465324818485460">"安全"</string>
安全选项界面
<activity android:name="Settings$SecuritySettingsActivity"
android:label="@string/security_settings_title"
android:configChanges="orientation|keyboardHidden|screenSize"
android:taskAffinity=""
android:parentActivityName="Settings">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.settings.SECURITY_SETTINGS" />
<action android:name="android.credentials.UNLOCK" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" />
</intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.SecuritySettings" />
<meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
android:resource="@id/security_settings" />
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true" />
</activity>
别名设置:
<activity-alias android:name="SecuritySettings"
android:label="@string/security_settings_title"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true"
android:targetActivity="Settings$SecuritySettingsActivity">
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.SecuritySettings" />
<meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
android:resource="@id/security_settings" />
</activity-alias>
所以找到SecuritySettings.java,这里是“设置”->"安全"界面,也是开始寻找如何修改默认锁屏方式的入口。
第二阶段:寻找判断的过程
决定分支走向显示“滑动”的判断
if (!lockPatternUtils.isSecure()) {
// if there are multiple users, disable "None" setting
UserManager mUm = (UserManager) context. getSystemService(Context.USER_SERVICE);
List<UserInfo> users = mUm.getUsers(true);
final boolean singleUser = users.size() == 1;
if (singleUser && lockPatternUtils.isLockScreenDisabled()) {
resid = R.xml.security_settings_lockscreen;
} else {
resid = R.xml.security_settings_chooser;
}
}
经过查看xml文件发现蓝色的是“滑动”的界面,随后会根据resid判断加载哪个xml:
addPreferencesFromResource(resid)
得到默认锁屏方式是“滑动”的条件:
(1)lockPatternUtils.isSecure() == false
(2)singleUser && lockPatternUtils.isLockScreenDisabled() == false
singleUser在单用户为true,主要看isLockScreenDisabled()是否为false
根据lockPatternUtils类型找到对应文件:
./frameworks/base/core/java/com/android/internal/widget/LockPatternUtils.java
为了isLockScreenDisabled()为false,getLong(DISABLE_LOCKSCREEN_KEY, 0)应该为0,或者在多用户模式特定情况下(当前情况不是,而且也暂未遇过),所以此处认为决定因素就是getLong(DISABLE_LOCKSCREEN_KEY, 0)的取值。
public boolean isLockScreenDisabled() {
getLong(DISABLE_LOCKSCREEN_KEY, 0) != 0) {
// Check if the number of switchable users forces the lockscreen.
final List<UserInfo> users = UserManager.get(mContext).getUsers(true);
final int userCount = users.size();
int switchableUsers = 0;
for (int i = 0; i < userCount; i++) {
if (users.get(i).supportsSwitchTo()) {
switchableUsers++;
}
}
return switchableUsers < 2;
}
return false;
}
(public final static String DISABLE_LOCKSCREEN_KEY = "lockscreen.disabled";)
锁屏方式为“无”或“滑动”是在isSecure()为false,如果要修改默认锁屏方式从“无”到“滑动”,或者“滑动”到“无”,蓝色得到的
getLong()方法:
private long getLong(String secureSettingKey, long defaultValue) {
try {
getLockSettings().getLong(secureSettingKey, defaultValue,
getCurrentOrCallingUserId());
} catch (RemoteException re) {
return defaultValue;
}
}
调用:
private ILockSettings
if (mLockSettingsService == null) {
ILockSettings service = ILockSettings.Stub.asInterface(
ServiceManager.getService("lock_settings"));
mLockSettingsService = service;
}
return mLockSettingsService;
}
对应服务(就是把ILockSettings去掉前面的I后再搜索相关文件,如果想详细点后面加上Service再找):
./frameworks/base/services/core/java/com/android/server/LockSettingsService.java
public long getLong(String key, long defaultValue, int userId) throws RemoteException {
checkReadPermission(key, userId);
mStorage.readKeyValue(key, null, userId);
return TextUtils.isEmpty(value) ? defaultValue : Long.parseLong(value);
}
(private final LockSettingsStorage mStorage;)
./frameworks/base/services/core/java/com/android/server/LockSettingsStorage.java
public String readKeyValue(String key, String defaultValue, int userId) {
int version;
synchronized (mCache) {
if (mCache.hasKeyValue(key, userId)) {
return mCache.peekKeyValue(key, defaultValue, userId);
}
version = mCache.getVersion();
}
Cursor cursor;
Object result = DEFAULT;
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
if ((cursor = db.query(TABLE, COLUMNS_FOR_QUERY,
COLUMN_USERID + "=? AND " + COLUMN_KEY + "=?",
new String[] { Integer.toString(userId), key },
null, null, null)) != null)
if (cursor.moveToFirst()) {
result = cursor.getString(0);
}
cursor.close();
}
mCache.putKeyValueIfUnchanged(key, result, userId, version);
return result == DEFAULT ? defaultValue : (String) result;
}
(private static final String TABLE = "locksettings";)
查询locksettings表的值lockscreen.disabled
对应文件/data/system/locksettings.db
我想直接改db文件的值应该也可以,不过这样改会使整个锁屏的逻辑放生变化,锁屏方式所不属于disable都发生改变从词意上也说不通,所以除非想试着玩还是别这么改。
数据库的lockscreen.disabled值的设置:
相关文件:
frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
frameworks/base/core/java/android/provider/Settings.java: public static final String LOCKSCREEN_DISABLED = "lockscreen.disabled";
DatabaseHelper.java文件:
ro.lockscreen.disable.default", false) == true) {
loadSetting(stmt, Settings.System.LOCKSCREEN_DISABLED, "1");
} else {
loadBooleanSetting(stmt, Settings.System.LOCKSCREEN_DISABLED,
R.bool.def_lockscreen_disabled);
}
private void loadBooleanSetting(SQLiteStatement stmt, String key, int resid) {
loadSetting(stmt, key,
mContext.getResources().getBoolean(resid) ? "1" : "0");
}
所以比较合适的修改是在ro.lockscreen.disable.default和def_lockscreen_disabled的值,这个肯定和原生及其他定制不同,应该看自己的实际代码(也许判断都不同)。
ro.locksreeen.disable.default的值在系统属性中,要查找具体的生成之处。
R.bool.def_lockscreen_disabled为false则lockscreen.disabled值为0,否则为1
R.bool.def_lockscreen_disabled对应文件:
./frameworks/base/packages/SettingsProvider/res/values/defaults.xml: <bool name="def_lockscreen_disabled">false</bool>
可能在定制系统的overlay里有相应的修改:
./device/generic/x86/overlay/frameworks/base/packages/SettingsProvider/res/values/defaults.xml: <bool name="def_lockscreen_disabled">true</bool>
./device/generic/armv7-a-neon/overlay/frameworks/base/packages/SettingsProvider/res/values/defaults.xml: <bool name="def_lockscreen_disabled">true</bool>