最近投影儀升級到9.0的系統(tǒng)角钩,在開發(fā)Launcher的時候止状,發(fā)現(xiàn)開機動畫從part1到part2再到home時中間會有一段時間黑屏币励,那是為什么會出現(xiàn)這種情況呢?
引起這個原因主要是Android 7.0 引入的Direct boot
模式導(dǎo)致的掸冤,而此時我們自己開發(fā)的 Launcher 并沒有兼容這種模式厘托,所以系統(tǒng)為了兼容舊的未支持 Direct boot
的app,會出現(xiàn)短暫黑屏的現(xiàn)象稿湿。
Direct boot
模式介紹
當設(shè)備已開機但用戶尚未解鎖設(shè)備時铅匹,Android 7.0 將在安全的Direct boot
模式下運行。為支持此模式饺藤,系統(tǒng)為數(shù)據(jù)提供了兩個存儲位置:
- Credential encrypted storage (憑據(jù)加密存儲)包斑,這是默認存儲位置,僅在用戶解鎖設(shè)備后可用涕俗。
- Device encrypted storage (設(shè)備加密存儲)罗丰,該存儲位置在
Direct boot
模式下和用戶解鎖設(shè)備后均可使用。
默認情況下再姑,應(yīng)用不會在Direct boot
模式下運行萌抵。如果您的應(yīng)用需要在Direct boot
模式下執(zhí)行操作,您可以注冊應(yīng)在此模式下運行的應(yīng)用組件元镀。需要在Direct boot
模式下運行的一些常見應(yīng)用用例包括:
- 已安排通知的應(yīng)用绍填,如鬧鐘應(yīng)用。
- 提供重要用戶通知的應(yīng)用栖疑,如短信應(yīng)用讨永。
- 提供無障礙服務(wù)的應(yīng)用,如 Talkback遇革。
如果應(yīng)用在Direct boot
模式下運行時需要訪問數(shù)據(jù)卿闹,請使用設(shè)備加密存儲。設(shè)備加密存儲包含使用密鑰加密的數(shù)據(jù)澳淑,只有設(shè)備成功執(zhí)行驗證啟動后數(shù)據(jù)才可用比原。
對于應(yīng)使用與用戶憑據(jù)(如 PIN 碼或密碼)關(guān)聯(lián)的密鑰加密的數(shù)據(jù),請使用憑據(jù)加密存儲杠巡。憑據(jù)加密存儲僅在用戶成功解鎖設(shè)備后可用,直到用戶再次重啟設(shè)備雇寇。如果用戶在解鎖設(shè)備后啟用鎖定屏幕氢拥,則不會鎖定憑據(jù)加密存儲蚌铜。
APP如何在Direct boot
模式下運行?
應(yīng)用必須先向系統(tǒng)注冊其組件嫩海,然后才能在Direct boot
模式下運行或訪問設(shè)備加密存儲冬殃。應(yīng)用通過將組件標記為加密感知來向系統(tǒng)注冊。要將組件標記為加密感知叁怪,請在清單中將 android:directBootAware
屬性設(shè)置為 true
审葬。
當設(shè)備重啟后,加密感知組件可以注冊以接收來自系統(tǒng)的 ACTION_LOCKED_BOOT_COMPLETED
廣播消息奕谭。此時涣觉,設(shè)備加密存儲可用,您的組件可以執(zhí)行需要在Direct boot
模式下運行的任務(wù)血柳,例如觸發(fā)已設(shè)定的鬧鈴官册。
以下代碼段示例說明了如何在應(yīng)用清單中將 BroadcastReceiver
注冊為加密感知并為 ACTION_LOCKED_BOOT_COMPLETED
添加 intent 過濾器:
<receiver
android:directBootAware="true" >
...
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
在用戶解鎖設(shè)備后,所有組件均可訪問設(shè)備加密存儲和憑據(jù)加密存儲难捌。
訪問設(shè)備加密存儲(Device encrypted storage)
要訪問設(shè)備加密存儲膝宁,請通過調(diào)用 Context
創(chuàng)建另一個 Context.createDeviceProtectedStorageContext()
實例。通過此上下文發(fā)出的所有存儲 API 調(diào)用均訪問設(shè)備加密存儲根吁。以下示例會訪問設(shè)備加密存儲并打開現(xiàn)有的應(yīng)用數(shù)據(jù)文件:
Context directBootContext = appContext.createDeviceProtectedStorageContext();
// Access appDataFilename that lives in device encrypted storage
FileInputStream inStream = directBootContext.openFileInput(appDataFilename);
// Use inStream to read content...
僅針對在Direct boot
模式下必須可以訪問的信息使用設(shè)備加密存儲员淫。請勿將設(shè)備加密存儲用作通用加密存儲。對于私密用戶信息击敌,或在Direct boot
模式下不需要的加密數(shù)據(jù)满粗,請使用憑據(jù)加密存儲。
接收用戶解鎖通知
當用戶在重啟后解鎖設(shè)備時愚争,應(yīng)用可以切換至訪問憑據(jù)加密存儲映皆,并使用依賴用戶憑據(jù)的常規(guī)系統(tǒng)服務(wù)。
為了在重啟后用戶解鎖設(shè)備時收到通知轰枝,請從正在運行的組件注冊 BroadcastReceiver
以監(jiān)聽解鎖通知消息捅彻。在用戶重啟后解鎖設(shè)備時:
- 如果應(yīng)用具有需要立即通知的前臺進程,請監(jiān)聽
ACTION_USER_UNLOCKED
消息鞍陨。 - 如果應(yīng)用僅使用可以對延遲通知執(zhí)行操作的后臺進程步淹,請監(jiān)聽
ACTION_BOOT_COMPLETED
消息。
如果已解鎖設(shè)備诚撵,您可以通過調(diào)用 UserManager.isUserUnlocked()
來了解情況缭裆。
遷移現(xiàn)有數(shù)據(jù)
如果用戶將其設(shè)備更新為使用Direct boot
模式,您可能需要將現(xiàn)有數(shù)據(jù)遷移到設(shè)備加密存儲寿烟。請使用 Context.moveSharedPreferencesFrom()
和 Context.moveDatabaseFrom()
在憑據(jù)加密存儲與設(shè)備加密存儲之間遷移偏好設(shè)置和數(shù)據(jù)庫數(shù)據(jù)澈驼。
請自行判斷要從憑據(jù)加密存儲向設(shè)備加密存儲遷移哪些數(shù)據(jù)。請勿將私密用戶信息(如密碼或授權(quán)令牌)遷移到設(shè)備加密存儲筛武。在某些情況下缝其,您可能需要在這兩種加密存儲中管理單獨的數(shù)據(jù)集挎塌。
Launcher非 directboot
時為什么會黑屏?
因為在系統(tǒng)啟動過程中内边,會去尋找支持 Direct boot
的 HOME 應(yīng)用榴都,而此時的Launcher是不支持的,這個時候系統(tǒng)會啟動 FallbackHome
漠其,之后再啟動Launcher嘴高,FallbackHome
又是什么東東?
FallbackHome
介紹
FallbackHome
屬于 Settings
中的一個 activity
和屎,Settings 的 android:directBootAware
為 true
拴驮,并且 FallbackHome
在 category 中配置了Home屬性,而Launcher的 android:directBootAware
為 false
眶俩,所有只有 FallbackHome
可以在 direct boot
模式下啟動
<application android:label="@string/settings_label"
android:icon="@mipmap/ic_launcher_settings"
............
android:directBootAware="true">
<!-- Triggered when user-selected home app isn't encryption aware -->
<activity android:name=".FallbackHome"
android:excludeFromRecents="true"
android:theme="@style/FallbackHome">
<intent-filter android:priority="-1000">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
所以在 ActivityManagerService
啟動Home界面時莹汤,從 PackageManagerService
中獲取到的Home界面就是 FallbackHome
Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
return intent;
}
boolean startHomeActivityLocked(int userId, String reason) {
if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {
// We are running in factory test mode, but unable to find
// the factory test app, so just sit around displaying the
// error message and don't try to start anything.
return false;
}
Intent intent = getHomeIntent();
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); //獲取Home activity信息
if (aInfo != null) {
intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
// Don't do this if the home app is currently being
// instrumented.
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason); //啟動FallbackHome
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
接著就會將 FallbackHome
啟動起來,是個透明的activity颠印,代碼很簡單纲岭,不到100行,創(chuàng)建 FallbackHome
時注冊 ACTION_USER_UNLOCKED
廣播线罕,然后進行判斷用戶是否都已經(jīng)解鎖止潮,如果沒有就結(jié)束執(zhí)行。之后就會等待接收 ACTION_USER_UNLOCKED
廣播钞楼,繼續(xù)判斷用戶是否已經(jīng)解鎖喇闸,如果此時已經(jīng)解鎖,就找Home界面询件,如果沒有找到就發(fā)延遲消息500ms再找一次燃乍,如果找到Launcher就會將 FallbackHome
finish掉。
下面就要看具體什么時候發(fā)送 ACTION_USER_UNLOCKED
廣播了宛琅。
代碼位置packages/apps/Settings/src/com/android/settings/FallbackHome.java
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import android.app.Activity;
import android.app.WallpaperColors;
import android.app.WallpaperManager;
import android.app.WallpaperManager.OnColorsChangedListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ResolveInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import java.util.Objects;
public class FallbackHome extends Activity {
private static final String TAG = "FallbackHome";
private static final int PROGRESS_TIMEOUT = 2000;
private boolean mProvisioned;
private WallpaperManager mWallManager;
private final Runnable mProgressTimeoutRunnable = () -> {
View v = getLayoutInflater().inflate(
R.layout.fallback_home_finishing_boot, null /* root */);
setContentView(v);
v.setAlpha(0f);
v.animate()
.alpha(1f)
.setDuration(500)
.setInterpolator(AnimationUtils.loadInterpolator(
this, android.R.interpolator.fast_out_slow_in))
.start();
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
};
private final OnColorsChangedListener mColorsChangedListener = new OnColorsChangedListener() {
@Override
public void onColorsChanged(WallpaperColors colors, int which) {
if (colors != null) {
final View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
updateVisibilityFlagsFromColors(colors, decorView.getSystemUiVisibility()));
mWallManager.removeOnColorsChangedListener(this);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set ourselves totally black before the device is provisioned so that
// we don't flash the wallpaper before SUW
mProvisioned = Settings.Global.getInt(getContentResolver(),
Settings.Global.DEVICE_PROVISIONED, 0) != 0;
final int flags;
if (!mProvisioned) {
setTheme(R.style.FallbackHome_SetupWizard);
flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
} else {
flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
}
mWallManager = getSystemService(WallpaperManager.class);
if (mWallManager == null) {
Log.w(TAG, "Wallpaper manager isn't ready, can't listen to color changes!");
} else {
loadWallpaperColors(flags);
}
getWindow().getDecorView().setSystemUiVisibility(flags);
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
maybeFinish();
}
@Override
protected void onResume() {
super.onResume();
if (mProvisioned) {
mHandler.postDelayed(mProgressTimeoutRunnable, PROGRESS_TIMEOUT);
}
}
@Override
protected void onPause() {
super.onPause();
mHandler.removeCallbacks(mProgressTimeoutRunnable);
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
if (mWallManager != null) {
mWallManager.removeOnColorsChangedListener(mColorsChangedListener);
}
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
maybeFinish();
}
};
private void loadWallpaperColors(int flags) {
final AsyncTask loadWallpaperColorsTask = new AsyncTask<Object, Void, Integer>() {
@Override
protected Integer doInBackground(Object... params) {
final WallpaperColors colors =
mWallManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
// Use a listener to wait for colors if not ready yet.
if (colors == null) {
mWallManager.addOnColorsChangedListener(mColorsChangedListener,
null /* handler */);
return null;
}
return updateVisibilityFlagsFromColors(colors, flags);
}
@Override
protected void onPostExecute(Integer flagsToUpdate) {
if (flagsToUpdate == null) {
return;
}
getWindow().getDecorView().setSystemUiVisibility(flagsToUpdate);
}
};
loadWallpaperColorsTask.execute();
}
private void maybeFinish() {
if (getSystemService(UserManager.class).isUserUnlocked()) {
final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME);
final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {
if (UserManager.isSplitSystemUser()
&& UserHandle.myUserId() == UserHandle.USER_SYSTEM) {
// This avoids the situation where the system user has no home activity after
// SUW and this activity continues to throw out warnings. See b/28870689.
return;
}
Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
mHandler.sendEmptyMessageDelayed(0, 500);
} else {
Log.d(TAG, "User unlocked and real home found; let's go!");
getSystemService(PowerManager.class).userActivity(
SystemClock.uptimeMillis(), false);
finish();
}
}
}
// Set the system ui flags to light status bar if the wallpaper supports dark text to match
// current system ui color tints.
private int updateVisibilityFlagsFromColors(WallpaperColors colors, int flags) {
if ((colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) {
return flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
| View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
}
return flags & ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
& ~(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
maybeFinish();
}
};
}
可以在線查看
解鎖后就進入到Launcher了刻蟹,至于 ACTION_USER_UNLOCKED
廣播是怎么發(fā)送的,大家可以再自行研究了嘿辟。
參考鏈接
https://developer.android.com/training/articles/direct-boot?hl=zh-cn
https://blog.csdn.net/ws6013480777777/article/details/86662739
https://blog.csdn.net/fu_kevin0606/article/details/65437594