android13#settings#reset options

500 阅读23分钟

1.简介

学习下settings》system》reset options相关

2.reset options

image.png

2.1.system_dashboard_fragment.xml

    <Preference
        android:key="reset_dashboard"
        android:title="@string/reset_dashboard_title"
        android:icon="@drawable/ic_restore"
        android:order="-30"
        android:fragment="com.android.settings.system.ResetDashboardFragment"
        settings:controller="com.android.settings.system.ResetPreferenceController"/>

>1.ResetPreferenceController

reset options是否可用是配置里的字段控制的

    public int getAvailabilityStatus() {
        return mContext.getResources().getBoolean(R.bool.config_show_reset_dashboard)
                ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

2.2.ResetDashboardFragment

这个就是点击reset options以后跳转的页面

>1.reset_dashboard_fragment.xml

image.png

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/reset_dashboard_title">

    <!-- Network reset ,跳转到fragment见小节3-->
    <com.android.settingslib.RestrictedPreference
        android:key="network_reset_pref"
        android:title="@string/reset_network_title"
        settings:userRestriction="no_network_reset"
        settings:useAdminDisabledSummary="true"
        android:fragment="com.android.settings.ResetNetwork" />

    <!-- Reset app preferences -->
    <Preference
        android:key="reset_app_prefs"
        android:title="@string/reset_app_preferences" />

    <!-- Erase Euicc data -->
    <Preference
        android:key="erase_euicc_data"
        android:title="@string/reset_esim_title"
        settings:controller="com.android.settings.network.EraseEuiccDataController" />

    <!-- Factory reset -->
    <com.android.settingslib.RestrictedPreference
        android:key="factory_reset"
        android:title="@string/main_clear_title"
        settings:keywords="@string/keywords_factory_data_reset"
        settings:userRestriction="no_factory_reset"
        settings:useAdminDisabledSummary="true"
        android:fragment="com.android.settings.MainClear" />
</PreferenceScreen>

>2.buildPreferenceControllers

相关的controller,具体见2.3

    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
            Lifecycle lifecycle) {
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        controllers.add(new NetworkResetPreferenceController(context));
        controllers.add(new FactoryResetPreferenceController(context));
        controllers.add(new ResetAppPrefPreferenceController(context, lifecycle));
        return controllers;
    }

2.3.Controller

>1.NetworkResetPreferenceController

网络重置

    public NetworkResetPreferenceController(Context context) {
        super(context);
        mRestrictionChecker = new NetworkResetRestrictionChecker(context);
    }

    @Override
    public boolean isAvailable() {//功能是否可用
        return !mRestrictionChecker.hasUserRestriction();
    }

>2.ResetAppPrefPreferenceController

重置app数据的控制器

    public boolean handlePreferenceTreeClick(Preference preference) {
        if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
            return false;
        }
        mResetAppsHelper.buildResetDialog();//显示一个dialog
        return true;
    }

    @Override
    public boolean isAvailable() {
        return true;//默认可用
    }

//点击是弹出一个对话框,点击事件见2.4

    void buildResetDialog() {
        if (mResetDialog == null) {
            mResetDialog = new AlertDialog.Builder(mContext)
                    .setTitle(R.string.reset_app_preferences_title)
                    .setMessage(R.string.reset_app_preferences_desc)
                    .setPositiveButton(R.string.reset_app_preferences_button, this)
                    .setNegativeButton(R.string.cancel, null)
                    .setOnDismissListener(this)
                    .show();
        }
    }

>3.FactoryResetPreferenceController

恢复出厂设置的控制器

    public FactoryResetPreferenceController(Context context) {
        super(context);
        mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
    }

    /**系统用户或者测试用户可用*/
    @Override
    public boolean isAvailable() {
        return mUm.isAdminUser() || Utils.isDemoUser(mContext);
    }

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (KEY_FACTORY_RESET.equals(preference.getKey())) {
        //点击跳转到其他activity,见小节4
            final Intent intent = new Intent(mContext, Settings.FactoryResetActivity.class);
            mContext.startActivity(intent);
            return true;
        }
        return false;
    }

>4.EraseEuiccDataController

这个是esim卡相关的,有对应的功能才可见

    public boolean handlePreferenceTreeClick(Preference preference) {
        if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
            return false;
        }
        //选项的点击事件
        EraseEuiccDataDialogFragment.show(mHostFragment);
        return true;
    }

    @Override
    public int getAvailabilityStatus() {
    //是否可用,
        return mContext.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_TELEPHONY_EUICC) ? AVAILABLE_UNSEARCHABLE
                : UNSUPPORTED_ON_DEVICE;
    }

2.4.ResetAppsHelper

>1.构造方法

看下补充2里用到的对象都是啥

    public ResetAppsHelper(Context context) {
        mContext = context;
        mPm = context.getPackageManager();
        mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        mNm = INotificationManager.Stub.asInterface(
                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
        mNpm = NetworkPolicyManager.from(context);
        mAom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
    }

>2.onClick

2.3.2 ,reset app的点击事件

    public void onClick(DialogInterface dialog, int which) {
        if (mResetDialog != dialog) {
            return;
        }
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
            //获取本地配置列表,哪些app可以不用reset
                final List<String> allowList = Arrays.asList(
                        mContext.getResources().getStringArray(
                                R.array.config_skip_reset_apps_package_name));
                for (UserHandle userHandle : mUm.getEnabledProfiles()) {
                    final int userId = userHandle.getIdentifier();
                    //获取所有的安装包
                    final List<ApplicationInfo> apps = mPm.getInstalledApplicationsAsUser(
                            PackageManager.GET_DISABLED_COMPONENTS, userId);
                    for (int i = 0; i < apps.size(); i++) {
                        ApplicationInfo app = apps.get(i);
                        if (allowList.contains(app.packageName)) {
                        //白名单里的,不用reset的,继续
                            continue;
                        }
                        try {
                        //清除通知相关的数据,设定等,见下篇分析
                            mNm.clearData(app.packageName, app.uid, false);
                        } catch (android.os.RemoteException ex) {
                        }
                        if (!app.enabled) {
                            try {
                            //如果app不可用,恢复为默认的状态
                                if (mIPm.getApplicationEnabledSetting(app.packageName, userId)
                                        == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
                                    mIPm.setApplicationEnabledSetting(app.packageName,
                                            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                                            PackageManager.DONT_KILL_APP,
                                            userId,
                                            mContext.getPackageName());
                                }
                            } 
                        }
                    }
                }
                try {
                //清除应用的权限,喜好等,比如camera里的视频,点击播放,可能有多个播放app供选择,
                //我们选择了一种,勾选always,以后就直接用这个打开了,这里就会清除这种记录
                    mIPm.resetApplicationPreferences(UserHandle.myUserId());
                } catch (RemoteException e) {
                }
                //见补充3,重置所有的ops
                mAom.resetAllModes();
                //
                BatteryOptimizeUtils.resetAppOptimizationMode(mContext, mIPm, mAom);
                //查找后台网络受限的uids
                final int[] restrictedUids = mNpm.getUidsWithPolicy(
                        POLICY_REJECT_METERED_BACKGROUND);
                final int currentUserId = ActivityManager.getCurrentUser();
                for (int uid : restrictedUids) {
                    //如果是当前用户,
                    if (UserHandle.getUserId(uid) == currentUserId) {
                    //策略设置为空
                        mNpm.setUidPolicy(uid, POLICY_NONE);
                    }
                }
            }
        });
    }

>3.AppOpsManager.java

      public void resetAllModes() {
          try {
          //下篇分析
              mService.resetAllModes(mContext.getUserId(), null);
          }
      }

3.ResetNetwork

image.png

public class ResetNetwork extends InstrumentedFragment {

3.1.reset_network.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:orientation="vertical" >
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_marginTop="12dp"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView //图上看到的文字就是这个
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:textDirection="locale"
                android:text="@string/reset_network_desc" />
            <include layout="@layout/reset_esim_checkbox"/>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
    <Spinner android:id="@+id/reset_network_subscription"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/initiate_reset_network"
        android:layout_gravity="end"
        android:layout_marginEnd="@dimen/reset_button_margin_end"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset_network_button_text"
        android:gravity="center"
        style="@style/ActionPrimaryButton"/>
</LinearLayout>

>1.reset_esim_checkbox.xml

  • 这个是擦除eSim信息的布局,没有ESim卡信息的话这里就不显示了
  • eSim卡就是没有实体卡, 参考:blog.csdn.net/qq_39420519…
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/erase_esim_container"
    style="@style/SudDescription"
    android:layout_marginTop="40dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:focusable="true"
    android:clickable="true"
    android:visibility="gone">

    <CheckBox
        android:id="@+id/erase_esim"
        style="@style/SudCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:focusable="false"
        android:clickable="false"
        android:checked="false"
        android:duplicateParentState="true"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            style="@style/TextAppearance.PreferenceTitle.SettingsLib"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reset_esim_title"/>

        <TextView
            style="?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reset_esim_desc"/>
    </LinearLayout>
</LinearLayout>

>2.reset_network_desc

<string name="reset_network_desc">This will reset all network settings, including:\n\n
<li>Wi\u2011Fi</li>\n
<li>Mobile data</li>\n
<li>Bluetooth</li>"</string>

3.2.onCreateView

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = (new ResetNetworkRestrictionViewBuilder(getActivity())).build();
        if (view != null) {
            return view;
        }
        //加载布局
        mContentView = inflater.inflate(R.layout.reset_network, null);
        //方法见3.4 ,数据见补充1
        establishInitialState(getActiveSubscriptionInfoList());
        return mContentView;
    }

>1.getActiveSubscriptionInfoList

blog.csdn.net/lyl0530/art…

  • 就是获取手机上的实体sim卡的信息。比如你手机上有移动联通两张卡,那么这里就会返回2个SubscriptionInfo
  • 当然这里返回的是active状态的,也就是卡是enable状态的
    private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
        SubscriptionManager mgr = getActivity().getSystemService(SubscriptionManager.class);
        if (mgr == null) {
            return Collections.emptyList();
        }
        return Optional.ofNullable(mgr.getActiveSubscriptionInfoList())
                .orElse(Collections.emptyList());
    }

3.3.onCreate

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(R.string.reset_network_title);
        //注册一个intent的launcher后边用
        mActivityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> onActivityLauncherResult(result));
    }

3.4.establishInitialState

这里说下subscription相关的知识:

  • subid的值从1开始,每插入一个新卡,subId的值就会加1,它是数据库telephony.db的主键,拔卡重插不会增加新的数据。subid也不会变。
  • slotid或者phoneid是指卡槽,双卡机器的卡槽1值为0,卡槽2值为1,更多的卡槽的话id自增
  • getDefaultVoiceSubscriptionId 带有语音功能的,subId返回的就是这个
  • getDefaultDataSubscriptionId 只支持数据功能的,subId返回的就是这个,感觉就是流量卡,不能打电话的那种,如果是只支持语音功能的,那么返回-1
  • getDefaultSmsSubscriptionId 如果卡只支持数据功能,返回-1
  • getDefaultSubscriptionId :支持语音功能,返回VoiceSubscriptionId,只只会数据功能,返回DataSubscriptionId,错误返回-1
    private void establishInitialState(List<SubscriptionInfo> subscriptionsList) {
        mSubscriptionSpinner = (Spinner) mContentView.findViewById(R.id.reset_network_subscription);
        //虚拟sim卡相关的容器以及checkbox,一般设备不用
        mEsimContainer = mContentView.findViewById(R.id.erase_esim_container);
        mEsimCheckbox = mContentView.findViewById(R.id.erase_esim);

        mSubscriptions = subscriptionsList;
        //有数据的话Spinner可见,没有数据的话不可见
        if (mSubscriptions != null && mSubscriptions.size() > 0) {
            //获取默认的 data subId
            int defaultSubscription = SubscriptionManager.getDefaultDataSubscriptionId();
            if (!SubscriptionManager.isUsableSubscriptionId(defaultSubscription)) {
            //id无效,重新获取voice sbu id
                defaultSubscription = SubscriptionManager.getDefaultVoiceSubscriptionId();
            }
            if (!SubscriptionManager.isUsableSubscriptionId(defaultSubscription)) {
            //还是无效,获取sms sub id
                defaultSubscription = SubscriptionManager.getDefaultSmsSubscriptionId();
            }
            if (!SubscriptionManager.isUsableSubscriptionId(defaultSubscription)) {
            //无效,返回默认的
                defaultSubscription = SubscriptionManager.getDefaultSubscriptionId();
            }

            int selectedIndex = 0;
            int size = mSubscriptions.size();
            List<String> subscriptionNames = new ArrayList<>();
            for (SubscriptionInfo record : mSubscriptions) {
                if (record.getSubscriptionId() == defaultSubscription) {
                    // Set the first selected value to the default
                    selectedIndex = subscriptionNames.size();
                }
                String name = SubscriptionUtil.getUniqueSubscriptionDisplayName(
                        record, getContext()).toString();
                if (TextUtils.isEmpty(name)) {
                    name = record.getNumber();
                }
                if (TextUtils.isEmpty(name)) {
                    CharSequence carrierName = record.getCarrierName();
                    name = TextUtils.isEmpty(carrierName) ? "" : carrierName.toString();
                }
                if (TextUtils.isEmpty(name)) {
                    name = String.format("MCC:%s MNC:%s Slot:%s Id:%s", record.getMcc(),
                            record.getMnc(), record.getSimSlotIndex(), record.getSubscriptionId());
                }
                subscriptionNames.add(name);
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_spinner_item, subscriptionNames);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mSubscriptionSpinner.setAdapter(adapter);
            mSubscriptionSpinner.setSelection(selectedIndex);
            if (mSubscriptions.size() > 1) {
                mSubscriptionSpinner.setVisibility(View.VISIBLE);
            } else {
                mSubscriptionSpinner.setVisibility(View.INVISIBLE);
            }
        } else {
            mSubscriptionSpinner.setVisibility(View.INVISIBLE);
        }
        //右侧button的点击事件
        mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_reset_network);
        mInitiateButton.setOnClickListener(mInitiateListener);
        
        if (showEuiccSettings(getContext())) {
        //没有虚拟sim卡的,不会走这里
            mEsimContainer.setVisibility(View.VISIBLE);
            mEsimContainer.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mEsimCheckbox.toggle();
                }
            });
        } else {
            mEsimCheckbox.setChecked(false /* checked */);
        }
    }

>1.showEuiccSettings

eUICC是否可用,可以理解为嵌入式的sim卡(也就是虚拟卡),是否显示相关设置

    private boolean showEuiccSettings(Context context) {
        EuiccManager euiccManager =
                (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
        //不可用
        if (!euiccManager.isEnabled()) {
            return false;
        }
        ContentResolver resolver = context.getContentResolver();
        //e-sim卡是否下载过配置,或者是否开发者模式允许
        return Settings.Global.getInt(resolver, Global.EUICC_PROVISIONED, 0) != 0
                || DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context);
    }

>2.mInitiateListener

    private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            //先验证keyguard
            if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
            //见3.5
                showFinalConfirmation();
            }
        }
    };

>3.runKeyguardConfirmation

    private boolean runKeyguardConfirmation(int request) {
        Resources res = getActivity().getResources();
        final ChooseLockSettingsHelper.Builder builder =
                new ChooseLockSettingsHelper.Builder(getActivity(), this);
        return builder.setRequestCode(request)
                .setTitle(res.getText(R.string.reset_network_title))
                .setActivityResultLauncher(mActivityResultLauncher)
                .show();
    }

3.5.showFinalConfirmation

跳到最终的确认页ResetNetworkConfirm.java,如下

image.png

    void showFinalConfirmation() {
        Bundle args = new Bundle();
    //要重置的网络选项
        ResetNetworkRequest request = new ResetNetworkRequest(
                ResetNetworkRequest.RESET_CONNECTIVITY_MANAGER |
                ResetNetworkRequest.RESET_VPN_MANAGER |
                ResetNetworkRequest.RESET_WIFI_MANAGER |
                ResetNetworkRequest.RESET_WIFI_P2P_MANAGER |
                ResetNetworkRequest.RESET_BLUETOOTH_MANAGER
        );
        if (mSubscriptions != null && mSubscriptions.size() > 0) {
        //如果手机有插卡的话会走这里,多张卡的话,reset的页面会让选择哪张卡
            int selectedIndex = mSubscriptionSpinner.getSelectedItemPosition();
            SubscriptionInfo subscription = mSubscriptions.get(selectedIndex);
            int subId = subscription.getSubscriptionId();
            request.setResetTelephonyAndNetworkPolicyManager(subId)
                   .setResetApn(subId);
        }
        if (mEsimContainer.getVisibility() == View.VISIBLE && mEsimCheckbox.isChecked()) {
        //国内好像没有虚拟卡吧?
            request.setResetEsim(getContext().getPackageName())
                   .writeIntoBundle(args);
        } else {
            request.writeIntoBundle(args);
        }

        new SubSettingLauncher(getContext())
        //跳转到这里
                .setDestination(ResetNetworkConfirm.class.getName())
                .setArguments(args)
                .setTitleRes(R.string.reset_network_confirm_title)
                .setSourceMetricsCategory(getMetricsCategory())
                .launch();
    }

4.FactoryResetActivity

Settings.java的内部类

    public static class FactoryResetActivity extends SettingsActivity {
        @Override
        protected void onCreate(Bundle savedState) {
            setTheme(SetupWizardUtils.getTheme(this, getIntent()));
            ThemeHelper.trySetDynamicColor(this);
            super.onCreate(savedState);
        }

        @Override
        protected boolean isToolbarEnabled() {
            return false;
        }
    }

4.1.清单文件

activity加载的fragment是下边的meta_data里配置的

        <activity android:name="Settings$FactoryResetActivity"
                  android:permission="android.permission.BACKUP"
                  android:label="@string/main_clear_title"
                  android:exported="true"
                  android:theme="@style/SudThemeGlif.Light">
            <intent-filter>
                <action android:name="com.android.settings.action.FACTORY_RESET"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                       android:value="com.android.settings.MainClear"/>
            <meta-data android:name="com.android.settings.HIGHLIGHT_MENU_KEY"
                       android:value="@string/menu_key_system"/>
        </activity>

5.MainClear

SettingsActivity前边有介绍过,上边的清单文件里有声明meta-data,所以会加载对应的fragment

public class MainClear extends InstrumentedFragment implements OnGlobalLayoutListener {

5.1.onCreateView

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final Context context = getContext();
        //是否允许reset操作
        final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
                UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
        final UserManager um = UserManager.get(context);
        final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
                .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
                        UserHandle.myUserId());
        if (disallow && !Utils.isDemoUser(context)) {
            return inflater.inflate(R.layout.main_clear_disallowed_screen, null);
        } else if (admin != null) {
            new ActionDisabledByAdminDialogHelper(getActivity())
                    .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
                    .setOnDismissListener(__ -> getActivity().finish())
                    .show();
            return new View(getContext());
        }
        //走到这里说明是允许
        mContentView = inflater.inflate(R.layout.main_clear, null);
        //见5.2,初始化操作
        establishInitialState();
        return mContentView;
    }

>1.main_clear.xml

<com.google.android.setupdesign.GlifLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/setup_wizard_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:icon="@drawable/ic_delete_accent"
    app:sucHeaderText="@string/main_clear_title">

    <ScrollView
        android:id="@+id/main_clear_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/main_clear_container"
            style="@style/SudContentFrame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:orientation="vertical">

            <TextView //见补充3
                style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:id="@+id/sud_layout_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/main_clear_desc"/>
            <TextView //见补充4
                android:id="@+id/also_erases_external"
                style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="@string/main_clear_desc_also_erases_external"/>
            <TextView //esim卡
                android:id="@+id/also_erases_esim"
                style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="@string/main_clear_desc_also_erases_esim"/>
            <TextView //你目前已登录如下账户
                android:id="@+id/accounts_label"
                style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="@string/main_clear_accounts"/>
            <LinearLayout //这里就是登录的账户信息,动态添加的
                android:id="@+id/accounts"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone">
                <!-- Do not add any children here as they will be removed in the MainClear.java
                    code. A list of accounts will be inserted programmatically. -->
            </LinearLayout>
            <TextView  //见补充6
                android:id="@+id/other_users_present"
                style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="@string/main_clear_other_users_present"/>
            <TextView //见补充7
                android:id="@+id/no_cancel_mobile_plan"
                style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:text="@string/main_clear_desc_no_cancel_mobile_plan"/>
            <TextView //补充8
                android:id="@+id/erase_external_option_text"
                style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/main_clear_desc_erase_external_storage"/>
            <LinearLayout
                android:id="@+id/erase_external_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:focusable="true"
                android:clickable="true">
                <CheckBox
                    android:id="@+id/erase_external"
                    style="@style/SudCheckBox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:focusable="false"
                    android:clickable="false"
                    android:duplicateParentState="true"/>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:orientation="vertical">
                    <TextView
                        style="@style/TextAppearance.PreferenceTitle.SettingsLib"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/erase_external_storage"/>
                    <TextView
                        style="?android:attr/textAppearanceListItemSecondary"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/erase_external_storage_description"/>
                </LinearLayout>
            </LinearLayout>

            <include layout="@layout/reset_esim_checkbox"/>

        </LinearLayout>
    </ScrollView>
</com.google.android.setupdesign.GlifLayout>

>2.图片

下图是2个文本控件,红线上下两个,字符串见补充3和4 image.png

>3.main_clear_desc

<string name="main_clear_desc" product="tablet" msgid="1651178880680056849">
"This will erase all data from your tablet’s "<b>"internal storage"</b>", including:\n\n"
<li>"Your Google Account"</li>\n
<li>"System and app data and settings"</li>\n
<li>"Downloaded apps"</li></string> 

>4.main_clear_desc_also_erases_external

<string name="main_clear_desc_also_erases_external" msgid="3687911419628956693">
<li>"Music"</li>\n
<li>"Photos"</li>\n
<li>"Other user data"</li></string>

>5.main_clear_accounts

 <string name="main_clear_accounts" product="default" msgid="7675859115108318537">
 \n\n"You are currently signed in to the following accounts:\n"</string>

>6.main_clear_other_users_present

 <string name="main_clear_other_users_present" product="default" msgid="2672976674798019077">
 \n\n"There are other users present on this device.\n"</string>

>7.main_clear_desc_no_cancel_mobile_plan

<string name="main_clear_desc_no_cancel_mobile_plan" msgid="369883568059127035">
\n\n"This will not cancel your mobile service plan."</string>

>8.main_clear_desc_erase_external_storage

<string name="main_clear_desc_erase_external_storage" product="nosdcard" msgid="4441604184663452046">
\n\n"To clear music, pictures and other user data, the "<b>"USB storage"</b>" needs to be erased."</string>

>9.log

这个是竖屏下打印的ScrollView的父容器结构,可以参考后边小节6里讲解的替换布局来理解

15:58:05.631  android.widget.ScrollView{6161e8b VFED.V... ......ID 0,0-1120,939 #7f0a0369 app:id/main_clear_scrollview}
15:58:05.631  android.widget.FrameLayout{b443b81 V.E...... ......ID 0,261-1120,1200 #7f0a05b7 app:id/sud_layout_content}
15:58:05.631  android.widget.LinearLayout{cf4d426 V.E...... ......ID 0,0-1120,1200}
15:58:05.631  com.google.android.setupdesign.view.BottomScrollView{8461867 VFED.V... ......ID 0,0-1120,1200 #7f0a05cd app:id/sud_scroll_view}
15:58:05.631  android.widget.LinearLayout{7f1f614 V.E...... ......ID 0,0-1120,1344 #7f0a05c5 app:id/sud_layout_template_content}
## 上边的布局见小节6.2.3竖屏
15:58:05.631  com.google.android.setupdesign.view.IntrinsicSizeFrameLayout{2780ebd V.E...... ......ID 40,204-1160,1548}
15:58:05.631  android.widget.LinearLayout{4ca0cb2 V.E...... ......ID 0,0-1200,1752 #7f0a05a1 app:id/suc_layout_status}
## 上边的布局见6.2.2
15:58:05.631  com.google.android.setupdesign.GlifLayout{d3be569 V.E...... ......ID 0,0-1200,1752 #7f0a0530 app:id/setup_wizard_layout}
15:58:05.631  android.widget.FrameLayout{32c88e4 V.E...... ......ID 0,0-1200,1752 #7f0a036a app:id/main_content}
15:58:05.631  android.widget.LinearLayout{d91d403 V.E...... ......ID 0,0-1200,1752}
15:58:05.631  android.widget.FrameLayout{3025f80 V.E...... ......ID 0,48-1200,1800 #7f0a0198 app:id/content_frame}
15:58:05.631  android.widget.LinearLayout{e1bd9b9 V.E...... ......ID 0,0-1200,1800 #7f0a0199 app:id/content_parent}
15:58:05.631  android.widget.FrameLayout{c4841fe V.E...... ......ID 0,0-1200,1800 #1020002 android:id/content}
15:58:05.631  android.widget.LinearLayout{753ad5f V.E...... ......ID 0,0-1200,1800}
15:58:05.631  DecorView@3edf3ac[Settings$FactoryResetActivity]
15:58:05.631  android.view.ViewRootImpl@f975875

5.2.establishInitialState

    void establishInitialState() {
        setUpActionBarAndTitle();
        setUpInitiateButton();

        mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
        mExternalStorage = mContentView.findViewById(R.id.erase_external);
        mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
        mEsimStorage = mContentView.findViewById(R.id.erase_esim);
        if (mScrollView != null) {
            mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        mScrollView = mContentView.findViewById(R.id.main_clear_scrollview);
        
       
        boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
        if (isExtStorageEmulated
                || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
         //外部存储器可用,隐藏相关的控件,正常都会走这里
            mExternalStorageContainer.setVisibility(View.GONE);

            final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
            externalOption.setVisibility(View.GONE);
        //显示 music,photos,other user data 文本框
            final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
            externalAlsoErased.setVisibility(View.VISIBLE);

            //没有意义,容器mExternalStorageContainer都不可见了
            mExternalStorage.setChecked(!isExtStorageEmulated);
        } else {
            mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                //没有外部存储器,点击容器切换checkbox状态
                    mExternalStorage.toggle();
                }
            });
        }
        //是否显示wipe sim卡内容
        if (showWipeEuicc()) {
            //系统属性,默认false
            if (showWipeEuiccCheckbox()) {
                mEsimStorageContainer.setVisibility(View.VISIBLE);
                mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEsimStorage.toggle();
                    }
                });
            } else {
                final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
                esimAlsoErased.setVisibility(View.VISIBLE);

                final View noCancelMobilePlan = mContentView.findViewById(
                        R.id.no_cancel_mobile_plan);
                noCancelMobilePlan.setVisibility(View.VISIBLE);
                mEsimStorage.setChecked(true /* checked */);
            }
        } else {
            mEsimStorage.setChecked(false /* checked */);
        }

        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
        //加载账户信息,布局里有个线性布局,这里动态添加内容
        loadAccountList(um);

        //
        mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
                    int oldScrollY) {
                if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
                    mInitiateButton.setEnabled(true);
                    mScrollView.setOnScrollChangeListener(null);
                }
            }
        });

        //监听布局改变,设置那个button的状态
        mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }
    //监听如下
    public void onGlobalLayout() {
        mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
    }

>1.setUpInitiateButton

    protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {

        public void onClick(View view) {
        //demouser cod ignore..
            //有密码的走这里,密码验证成功会走onActivityResult,如下
            if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
                return;
            }
            //没有密码的走这里
            //这个需要配置的,默认是没有配置的,返回是null
            Intent intent = getAccountConfirmationIntent();
            if (intent != null) {
                showAccountCredentialConfirmation(intent);
            } else {
            //见5.3,最终reset确认页面
                showFinalConfirmation();
            }
        }
    };

//

    void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
        if (!isValidRequestCode(requestCode)) {
            return;
        }

        if (resultCode != Activity.RESULT_OK) {
            establishInitialState();
            return;
        }

        Intent intent = null;
        //密码验证完,先判断是否有自定义账户的验证,有的话跳转到对应的页面
        if (CREDENTIAL_CONFIRM_REQUEST != requestCode
                && (intent = getAccountConfirmationIntent()) != null) {
            showAccountCredentialConfirmation(intent);
        } else {
        //没有自定义账户,直接跳到最终reset页面
        //见5.3
            showFinalConfirmation();
        }
    }

5.3.showFinalConfirmation

这个就是最终跳转的confirm页面,里边有个按钮点了就是真正的reset操作了

    void showFinalConfirmation() {
        final Bundle args = new Bundle();
        args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
        args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
        final Intent intent = new Intent();
        //要跳转的activity
        intent.setClass(getContext(),
                com.android.settings.Settings.FactoryResetConfirmActivity.class);
        //activity里加载的fragment
        intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, MainClearConfirm.class.getName());
        intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
        intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID,
                R.string.main_clear_confirm_title);
        intent.putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, getMetricsCategory());
        getContext().startActivity(intent);
    }

5.4.MainClearConfirm.java

>1.mFinalClickListener

最终的factory reset按钮的点击事件

    private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {

        public void onClick(View v) {
            // pre-flight check hardware support PersistentDataBlockManager
            //我们的设备 "ro.frp.pst" = /dev/block/bootdevice/by-name/frp
            if (SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("")) {
                return;
            }

            final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
                    getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);

            if (shouldWipePersistentDataBlock(pdbManager)) {
            //应该擦除持久的数据库
                new AsyncTask<Void, Void, Void>() {
                    int mOldOrientation;
                    ProgressDialog mProgressDialog;

                    @Override
                    protected Void doInBackground(Void... params) {
                        pdbManager.wipe();//核心就是这个了
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        mProgressDialog.hide();
                        if (getActivity() != null) {
                            getActivity().setRequestedOrientation(mOldOrientation);
                            doMainClear();
                        }
                    }

                    @Override
                    protected void onPreExecute() {
                        mProgressDialog = getProgressDialog();
                        mProgressDialog.show();

                        //擦除数据是耗时操作,固定屏幕方向
                        mOldOrientation = getActivity().getRequestedOrientation();
                        getActivity().setRequestedOrientation(
                                ActivityInfo.SCREEN_ORIENTATION_LOCKED);
                    }
                }.execute();
            } else {
                doMainClear();
            }
        }

        private ProgressDialog getProgressDialog() {
            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.setTitle(
                    getActivity().getString(R.string.main_clear_progress_title));
            progressDialog.setMessage(
                    getActivity().getString(R.string.main_clear_progress_text));
            return progressDialog;
        }
    }

>2.shouldWipePersistentDataBlock

下边是允许擦除数据的条件判断

    boolean shouldWipePersistentDataBlock(PersistentDataBlockManager pdbManager) {
        if (pdbManager == null) {
        //管理器为空
            return false;
        }
        // The persistent data block will persist if the device is still being provisioned.
        if (isDeviceStillBeingProvisioned()) {
        //设备还未provisioned
            return false;
        }
        // If OEM unlock is allowed, the persistent data block will be wiped during FR
        // process. If disabled, it will be wiped here instead.
        if (isOemUnlockedAllowed()) {
            return false;
        }
        final DevicePolicyManager dpm = (DevicePolicyManager) getActivity()
                .getSystemService(Context.DEVICE_POLICY_SERVICE);
        // Do not erase the factory reset protection data (from Settings) if factory reset
        // protection policy is not supported on the device.
        if (!dpm.isFactoryResetProtectionPolicySupported()) {
            return false;
        }
        // Do not erase the factory reset protection data (from Settings) if the
        // device is an organization-owned managed profile device and a factory
        // reset protection policy has been set.
        FactoryResetProtectionPolicy frpPolicy = dpm.getFactoryResetProtectionPolicy(null);
        if (dpm.isOrganizationOwnedDeviceWithManagedProfile() && frpPolicy != null
                && frpPolicy.isNotEmpty()) {
            return false;
        }
        return true;
    }

>3.doMainClear

    private void doMainClear() {
        Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
        intent.setPackage("android");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        intent.putExtra(Intent.EXTRA_REASON, "MainClearConfirm");
        intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
        intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
        getActivity().sendBroadcast(intent);
    }

>4.ACTION_FACTORY_RESET

        <receiver android:name="com.android.server.MasterClearReceiver"
            android:exported="true"
            android:permission="android.permission.MASTER_CLEAR">
            <intent-filter
                    android:priority="100" >
                <!-- For Checkin, Settings, etc.: action=FACTORY_RESET -->
                <action android:name="android.intent.action.FACTORY_RESET" />
                <!-- As above until all the references to the deprecated MASTER_CLEAR get updated to
                     FACTORY_RESET. -->
                <action android:name="android.intent.action.MASTER_CLEAR" />

                <!-- MCS always uses REMOTE_INTENT: category=MASTER_CLEAR -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="android.intent.category.MASTER_CLEAR" />
            </intent-filter>
        </receiver>

5.5.MasterClearReceiver

>1.onReceive

    public void onReceive(final Context context, final Intent intent) {

        final String factoryResetPackage = context
                .getString(com.android.internal.R.string.config_factoryResetPackage);
        if (Intent.ACTION_FACTORY_RESET.equals(intent.getAction())
                && !TextUtils.isEmpty(factoryResetPackage)) {
            //有自定义的factorey reset包,就交给自定义的处理,默认没有
            intent.setPackage(factoryResetPackage).setComponent(null);
            context.sendBroadcastAsUser(intent, UserHandle.SYSTEM);
            return;
        }

        final boolean shutdown = intent.getBooleanExtra("shutdown", false);
        final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
        mWipeExternalStorage = intent.getBooleanExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
        mWipeEsims = intent.getBooleanExtra(Intent.EXTRA_WIPE_ESIMS, false);
        final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false)
                || intent.getBooleanExtra(Intent.EXTRA_FORCE_FACTORY_RESET, false);

    //..
        //后台执行reboot,核心的 reset代码
        Thread thr = new Thread("Reboot") {
            @Override
            public void run() {
                try {//见小节10
                    RecoverySystem
                            .rebootWipeUserData(context, shutdown, reason, forceWipe, mWipeEsims);
                } 
            }
        };

        if (mWipeExternalStorage) {
            //如果要擦除外置卡数据的话,擦除完再执行reboot
            new WipeDataTask(context, thr).execute();
        } else {
            //执行reboot
            thr.start();
        }
    }

>2.doInBackground

            if (mWipeExternalStorage) {
                StorageManager sm = (StorageManager) mContext.getSystemService(
                        Context.STORAGE_SERVICE);
                sm.wipeAdoptableDisks();
            }

6.GlifLayout

external/setupdesign/main/src/com/google/android/setupdesign/GlifLayout.java

  • 这个是小节5用的根布局,自定义的帧布局,看下为啥有时候是全屏的,有时候是带边框非全屏的
  • 打印的布局结构为啥和5.1.1不一样,看下这个自定义控件是如何替换布局的。
public class GlifLayout extends PartnerCustomizationLayout {

6.1.init

构造方法里调用,这里就是处理第三方颜色,padding的

  private void init(AttributeSet attrs, int defStyleAttr) {
    if (isInEditMode()) {
      return;
    }

    TypedArray a =
        getContext().obtainStyledAttributes(attrs, R.styleable.SudGlifLayout, defStyleAttr, 0);
    boolean usePartnerHeavyTheme =
        a.getBoolean(R.styleable.SudGlifLayout_sudUsePartnerHeavyTheme, false);
    applyPartnerHeavyThemeResource = shouldApplyPartnerResource() && usePartnerHeavyTheme;
    //注册需要的对象
    registerMixin(HeaderMixin.class, new HeaderMixin(this, attrs, defStyleAttr));
    registerMixin(DescriptionMixin.class, new DescriptionMixin(this, attrs, defStyleAttr));
    registerMixin(IconMixin.class, new IconMixin(this, attrs, defStyleAttr));
    registerMixin(ProgressBarMixin.class, new ProgressBarMixin(this, attrs, defStyleAttr));
    registerMixin(IllustrationProgressMixin.class, new IllustrationProgressMixin(this));
    final RequireScrollMixin requireScrollMixin = new RequireScrollMixin(this);
    registerMixin(RequireScrollMixin.class, requireScrollMixin);
    //..

    ColorStateList primaryColor = a.getColorStateList(R.styleable.SudGlifLayout_sudColorPrimary);
    if (primaryColor != null) {
    //见6.5
      setPrimaryColor(primaryColor);
    }
    if (shouldApplyPartnerHeavyThemeResource()) {
    //rootView颜色设置
      updateContentBackgroundColorWithPartnerConfig();
    }

    View view = findManagedViewById(R.id.sud_layout_content);
    if (view != null) {
    //见7.2,是否使用第三方的资源
      if (shouldApplyPartnerResource()) {
      //设置第三方的padding,主要是左右padding
        LayoutStyler.applyPartnerCustomizationExtraPaddingStyle(view);
      }

      if (!(this instanceof GlifPreferenceLayout)) {
      //top padding设置
        tryApplyPartnerCustomizationContentPaddingTopStyle(view);
      }
    }
    //根据margion调整padding
    updateLandscapeMiddleHorizontalSpacing();
    
    //背景颜色设置,见6.5以及6.4,就是自己的第一个child的背景
    ColorStateList backgroundColor =
        a.getColorStateList(R.styleable.SudGlifLayout_sudBackgroundBaseColor);
    setBackgroundBaseColor(backgroundColor);
    //见6.5
    boolean backgroundPatterned =
        a.getBoolean(R.styleable.SudGlifLayout_sudBackgroundPatterned, true);
    setBackgroundPatterned(backgroundPatterned);

    final int stickyHeader = a.getResourceId(R.styleable.SudGlifLayout_sudStickyHeader, 0);
    if (stickyHeader != 0) {
      inflateStickyHeader(stickyHeader);
    }
    a.recycle();
  }

6.2.onInflateTemplate

这里定义的模板layout带主题

  protected View onInflateTemplate(LayoutInflater inflater, @LayoutRes int template) {
    if (template == 0) {
    //,默认使用的模板,不同尺寸指向不同的布局,具体在layout.xml里声明的,见补充1
      template = R.layout.sud_glif_template;
    }
    return inflateTemplate(inflater, R.style.SudThemeGlif_Light, template);
  }

>1.sud_glif_template

我们是平板,所以直接看大尺寸的即可

#values
<item name="sud_glif_template" type="layout">@layout/sud_glif_template_compact</item>
#values-sw600dp
<item name="sud_glif_template" type="layout">@layout/sud_glif_template_card</item>

>2.sud_glif_template_card

上下都是比重为1的view,所以中间的控件居中显示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/suc_layout_status"
    style="@style/SudGlifCardBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:visibility="invisible" />

    <com.google.android.setupdesign.view.IntrinsicSizeFrameLayout
        style="@style/SudGlifCardContainer"
        android:layout_width="@dimen/sud_glif_card_width"
        android:layout_height="wrap_content"
        android:height="@dimen/sud_glif_card_height">
        //见补充3,分横竖屏
        <include layout="@layout/sud_glif_template_content" />

    </com.google.android.setupdesign.view.IntrinsicSizeFrameLayout>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:visibility="invisible" />

</LinearLayout>

>3.横屏sud_glif_template_content

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sud_layout_template_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--header在左侧,content在右侧-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/sud_landscape_header_area"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="@dimen/sud_glif_land_header_area_weight"
            android:orientation="vertical">

            <ViewStub
                android:id="@+id/sud_layout_sticky_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <com.google.android.setupdesign.view.BottomScrollView
                android:id="@+id/sud_header_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                android:scrollIndicators="?attr/sudScrollIndicators">

                <include layout="@layout/sud_glif_header" />

            </com.google.android.setupdesign.view.BottomScrollView>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/sud_landscape_content_area"
            style="@style/SudLandContentContianerStyle"
            android:orientation="vertical">

            <com.google.android.setupdesign.view.BottomScrollView
                android:id="@+id/sud_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                android:scrollIndicators="?attr/sudScrollIndicators"
                android:importantForAccessibility="no">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
            <!--这个就是对外的container 小节6.3用到-->
                    <FrameLayout
                        android:id="@+id/sud_layout_content"
                        android:paddingTop="?attr/sudGlifContentPaddingTop"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1" />

                    <ViewStub
                        android:id="@+id/sud_layout_illustration_progress_stub"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:inflatedId="@+id/sud_layout_progress_illustration"
                        android:layout="@layout/sud_progress_illustration_layout" />
                </LinearLayout>

            </com.google.android.setupdesign.view.BottomScrollView>

        </LinearLayout>

    </LinearLayout>

    <ViewStub
        android:id="@+id/suc_layout_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

>3.竖屏sud_glif_template_content

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sud_layout_template_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ViewStub
        android:id="@+id/sud_layout_sticky_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!---->
    <com.google.android.setupdesign.view.BottomScrollView
        android:id="@+id/sud_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true"
        android:scrollIndicators="?attr/sudScrollIndicators"
        tools:ignore="UnusedAttribute">
    <!--header和content是垂直布局-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include layout="@layout/sud_glif_header" />

            <ViewStub
                android:id="@+id/sud_layout_illustration_progress_stub"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inflatedId="@+id/sud_layout_progress_illustration"
                android:layout="@layout/sud_progress_illustration_layout" />
    <!--这个就是对外的container 小节6.3用到-->
            <FrameLayout
                android:id="@+id/sud_layout_content"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

        </LinearLayout>

    </com.google.android.setupdesign.view.BottomScrollView>

    <ViewStub
        android:id="@+id/suc_layout_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

6.3.findContainer

这个容器的作用:把GlifLayout里原本的内容放到这个容器里

    protected ViewGroup findContainer(int containerId) {
      if (containerId == 0) {
        containerId = R.id.sud_layout_content;
      }
      return super.findContainer(containerId);
    }

6.4.updateBackground

用到的地方见6.5

  private void updateBackground() {
  //见6.2.2布局,GlifLayout里替换的根布局就是这个
    final View patternBg = findManagedViewById(R.id.suc_layout_status);
    if (patternBg != null) {
      int backgroundColor = 0;
      if (backgroundBaseColor != null) {
        backgroundColor = backgroundBaseColor.getDefaultColor();
      } else if (primaryColor != null) {
        backgroundColor = primaryColor.getDefaultColor();
      }
      Drawable background =
          backgroundPatterned
              ? new GlifPatternDrawable(backgroundColor)
              : new ColorDrawable(backgroundColor);
      //设置statusbar背景,就是patternBg,具体可以去StatusBarMixin查看
      getMixin(StatusBarMixin.class).setStatusBarBackground(background);
    }
  }

6.5.color设置

  public void setPrimaryColor(@NonNull ColorStateList color) {
    primaryColor = color;
    updateBackground();
    getMixin(ProgressBarMixin.class).setColor(color);
  }

  public void setBackgroundBaseColor(@Nullable ColorStateList color) {
    backgroundBaseColor = color;
    updateBackground();
  }

  /**
   * Sets whether the background should be {@link GlifPatternDrawable}. If {@code false}, the
   * background will be a solid color.
   */
  public void setBackgroundPatterned(boolean patterned) {
    backgroundPatterned = patterned;
    updateBackground();
  }

7.PartnerCustomizationLayout

external/setupcompat/main/java/com/google/android/setupcompat/PartnerCustomizationLayout.java

public class PartnerCustomizationLayout extends TemplateLayout {

7.1.init

构造方法里会调用

  private void init(AttributeSet attrs, int defStyleAttr) {
    if (isInEditMode()) {
      return;
    }

    TypedArray a =
        getContext()
            .obtainStyledAttributes(
                attrs, R.styleable.SucPartnerCustomizationLayout, defStyleAttr, 0);

    boolean layoutFullscreen =
        a.getBoolean(R.styleable.SucPartnerCustomizationLayout_sucLayoutFullscreen, true);

    a.recycle();

    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && layoutFullscreen) {
      setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    //这里注册了3个对象
    registerMixin(
        StatusBarMixin.class, new StatusBarMixin(this, activity.getWindow(), attrs, defStyleAttr));
    registerMixin(SystemNavBarMixin.class, new SystemNavBarMixin(this, activity.getWindow()));
    registerMixin(FooterBarMixin.class, new FooterBarMixin(this, attrs, defStyleAttr));

    getMixin(SystemNavBarMixin.class).applyPartnerCustomizations(attrs, defStyleAttr);

    if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
  }

7.2.shouldApplyPartnerResource

是否使用合作者的资源配置

  /** Returns if the current layout/activity applies partner customized configurations or not. */
  public boolean shouldApplyPartnerResource() {
    if (!enablePartnerResourceLoading()) {//默认为true,见补充1
      return false;
    }
    if (!usePartnerResourceAttr) {//见7.3
      return false;
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
      return false;
    }
    if (!PartnerConfigHelper.get(getContext()).isAvailable()) {
      return false;
    }
    return true;
  }

>1.enablePartnerResourceLoading

  protected boolean enablePartnerResourceLoading() {
    return true;
  }

7.3.onBeforeTemplateInflated

  protected void onBeforeTemplateInflated(AttributeSet attrs, int defStyleAttr) {

    usePartnerResourceAttr = true;

    activity = lookupActivityFromContext(getContext());
    //是否是setup流程
    boolean isSetupFlow = WizardManagerHelper.isAnySetupWizard(activity.getIntent());

    TypedArray a =
        getContext()
            .obtainStyledAttributes(
                attrs, R.styleable.SucPartnerCustomizationLayout, defStyleAttr, 0);
//setup流程中,或者布局配置里sucUsePartnerResource为true
    usePartnerResourceAttr =
        isSetupFlow
            || a.getBoolean(R.styleable.SucPartnerCustomizationLayout_sucUsePartnerResource, true);

    useDynamicColor = a.hasValue(R.styleable.SucPartnerCustomizationLayout_sucFullDynamicColor);
    useFullDynamicColorAttr =
        a.getBoolean(R.styleable.SucPartnerCustomizationLayout_sucFullDynamicColor, false);

    a.recycle();
  }

7.4.PartnerConfigHelper.java

>1.isAvailable

bundle不为空就返回true

  public boolean isAvailable() {
    return resultBundle != null && !resultBundle.isEmpty();
  }

>2.getPartnerConfigBundle

bundle数据来源是谷歌的setupwizard的contentProvider

  private void getPartnerConfigBundle(Context context) {
    if (resultBundle == null || resultBundle.isEmpty()) {
      try {
        resultBundle =
            context
                .getContentResolver()
                .call(
                    getContentUri(),
                    SUW_GET_PARTNER_CONFIG_METHOD,
                    /* arg= */ null,
                    /* extras= */ null);
        partnerResourceCache.clear();
      } 
    }
  }
public static final String SUW_GET_PARTNER_CONFIG_METHOD = "getOverlayConfig";

public static final String SUW_AUTHORITY = "com.google.android.setupwizard.partner";

  static Uri getContentUri() {
    return new Uri.Builder()
        .scheme(ContentResolver.SCHEME_CONTENT)
        .authority(SUW_AUTHORITY)
        .build();
  }

8.TemplateLayout

external/setupcompat/main/java/com/google/android/setupcompat/internal/TemplateLayout.java

public class TemplateLayout extends FrameLayout {

8.1.init

构造方法里调用

  private void init(int template, int containerId, AttributeSet attrs, int defStyleAttr) {
    final TypedArray a =
        getContext().obtainStyledAttributes(attrs, R.styleable.SucTemplateLayout, defStyleAttr, 0);
    if (template == 0) {
      template = a.getResourceId(R.styleable.SucTemplateLayout_android_layout, 0);
    }
    if (containerId == 0) {
      containerId = a.getResourceId(R.styleable.SucTemplateLayout_sucContainer, 0);
    }
    onBeforeTemplateInflated(attrs, defStyleAttr);
    inflateTemplate(template, containerId);

    a.recycle();
  }

>1.inflateTemplate

两个参数在子类都覆写了,如果为0,会给与默认值,见小节6.2,6.3

  private void inflateTemplate(int templateResource, int containerId) {
    final LayoutInflater inflater = LayoutInflater.from(getContext());
    final View templateRoot = onInflateTemplate(inflater, templateResource);
    //添加到当前容器里
    addViewInternal(templateRoot);
    //查找容器
    container = findContainer(containerId);
    if (container == null) {
      throw new IllegalArgumentException("Container cannot be null in TemplateLayout");
    }
    onTemplateInflated();
  }

根据template加载对应的布局view,子类重写了,给与默认的template,以及theme,见6.2

  protected View onInflateTemplate(LayoutInflater inflater, @LayoutRes int template) {
    return inflateTemplate(inflater, 0, template);
  }
  protected final View inflateTemplate(
      LayoutInflater inflater, @StyleRes int fallbackTheme, @LayoutRes int template) {
    if (template == 0) {
      throw new IllegalArgumentException("android:layout not specified for TemplateLayout");
    }
    if (fallbackTheme != 0) {
      inflater =
          LayoutInflater.from(new FallbackThemeWrapper(inflater.getContext(), fallbackTheme));
    }
    return inflater.inflate(template, this, false);
  }

8.2.addView

init方法里会获取container,可以看到最终的child实际加到这个容器里了,这里完成了替换,

  public void addView(View child, int index, ViewGroup.LayoutParams params) {
    container.addView(child, index, params);
  }

8.3.mixins

  private final Map<Class<? extends Mixin>, Mixin> mixins = new HashMap<>();

>1.registerMixin

数据注册见子类小节7.1

  protected <M extends Mixin> void registerMixin(Class<M> cls, M mixin) {
    mixins.put(cls, mixin);
  }

2.getMixin

  public <M extends Mixin> M getMixin(Class<M> cls) {
    return (M) mixins.get(cls);
  }

8.4.总结

  • 小节6到8都是GlifLayout布局相关,主要学习下它是如何替换内部child的
  • 主要就是内部自己加载了一套布局作为container,完事add到GlifLayout里,之后把FlifLayout里原本的child放到container里

9.FooterBarMixin

9.1.setPrimaryButton

  public void setPrimaryButton(FooterButton footerButton) {
    ensureOnMainThread("setPrimaryButton");
    ensureFooterInflated();

    // Setup button partner config
    FooterButtonPartnerConfig footerButtonPartnerConfig =
        new FooterButtonPartnerConfig.Builder(footerButton)
            .setPartnerTheme(
                getPartnerTheme(
                    footerButton,
                    /* defaultPartnerTheme= */ R.style.SucPartnerCustomizationButton_Primary,
                    /* buttonBackgroundColorConfig= */ PartnerConfig
                        .CONFIG_FOOTER_PRIMARY_BUTTON_BG_COLOR))
            .setButtonBackgroundConfig(PartnerConfig.CONFIG_FOOTER_PRIMARY_BUTTON_BG_COLOR)
            .setButtonDisableAlphaConfig(PartnerConfig.CONFIG_FOOTER_BUTTON_DISABLED_ALPHA)
            .setButtonDisableBackgroundConfig(PartnerConfig.CONFIG_FOOTER_BUTTON_DISABLED_BG_COLOR)
            .setButtonDisableTextColorConfig(
                PartnerConfig.CONFIG_FOOTER_PRIMARY_BUTTON_DISABLED_TEXT_COLOR)
            .setButtonIconConfig(getDrawablePartnerConfig(footerButton.getButtonType()))
            .setButtonRadiusConfig(PartnerConfig.CONFIG_FOOTER_BUTTON_RADIUS)
            .setButtonRippleColorAlphaConfig(PartnerConfig.CONFIG_FOOTER_BUTTON_RIPPLE_COLOR_ALPHA)
            .setTextColorConfig(PartnerConfig.CONFIG_FOOTER_PRIMARY_BUTTON_TEXT_COLOR)
            .setMarginStartConfig(PartnerConfig.CONFIG_FOOTER_PRIMARY_BUTTON_MARGIN_START)
            .setTextSizeConfig(PartnerConfig.CONFIG_FOOTER_BUTTON_TEXT_SIZE)
            .setButtonMinHeight(PartnerConfig.CONFIG_FOOTER_BUTTON_MIN_HEIGHT)
            .setTextTypeFaceConfig(PartnerConfig.CONFIG_FOOTER_BUTTON_FONT_FAMILY)
            .setTextStyleConfig(PartnerConfig.CONFIG_FOOTER_BUTTON_TEXT_STYLE)
            .build();
    //根据配置创建一个button,见9.2
    FooterActionButton button = inflateButton(footerButton, footerButtonPartnerConfig);
    // update information for primary button. Need to update as long as the button inflated.
    primaryButtonId = button.getId();
    button.setPrimaryButtonStyle(/* isPrimaryButtonStyle= */ true);
    primaryButton = footerButton;
    primaryButtonPartnerConfigForTesting = footerButtonPartnerConfig;
    //把button添加到容器里,见9.3
    onFooterButtonInflated(button, footerBarPrimaryBackgroundColor);
    onFooterButtonApplyPartnerResource(button, footerButtonPartnerConfig);

    // Make sure the position of buttons are correctly and prevent primary button create twice or
    // more.
    repopulateButtons();
  }

9.2.inflateButton

根据footerButton的配置,生成一个FooterActionButton

  private FooterActionButton inflateButton(
      FooterButton footerButton, FooterButtonPartnerConfig footerButtonPartnerConfig) {
    FooterActionButton button =
        createThemedButton(context, footerButtonPartnerConfig.getPartnerTheme());
    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
      button.setId(View.generateViewId());
    } else {
      button.setId(generateViewId());
    }

    // apply initial configuration into button view.
    button.setText(footerButton.getText());
    button.setOnClickListener(footerButton);
    button.setVisibility(footerButton.getVisibility());
    button.setEnabled(footerButton.isEnabled());
    button.setFooterButton(footerButton);

    footerButton.setOnButtonEventListener(createButtonEventListener(button.getId()));
    return button;
  }

9.3.onFooterButtonInflated

把按钮添加到容器里

  protected void onFooterButtonInflated(Button button, @ColorInt int defaultButtonBackgroundColor) {
    
    if (defaultButtonBackgroundColor != 0) {
      FooterButtonStyleUtils.updateButtonBackground(button, defaultButtonBackgroundColor);
    }
    //见补充,看下容器哪里来的
    buttonContainer.addView(button);
    autoSetButtonBarVisibility();
  }

9.4.ensureFooterInflated

  private LinearLayout ensureFooterInflated() {
    if (buttonContainer == null) {
      if (footerStub == null) {
        throw new IllegalStateException("Footer stub is not found in this template");
      }
      //见补充2
      buttonContainer = (LinearLayout) inflateFooter(R.layout.suc_footer_button_bar);
      onFooterBarInflated(buttonContainer);
      onFooterBarApplyPartnerResource(buttonContainer);
    }
    return buttonContainer;
  }

>1.footerStub

  public FooterBarMixin(
      TemplateLayout layout, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
    context = layout.getContext();
    //见6.2.3的布局末尾
    footerStub = layout.findManagedViewById(R.id.suc_layout_footer);

>2.inflateFooter

  protected View inflateFooter(@LayoutRes int footer) {
    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
      LayoutInflater inflater =
          LayoutInflater.from(
              new ContextThemeWrapper(context, R.style.SucPartnerCustomizationButtonBar_Stackable));
      footerStub.setLayoutInflater(inflater);
    }
    footerStub.setLayoutResource(footer);
    return footerStub.inflate();
  }

9.5.autoSetButtonBarVisibility

按钮的可见性处理

  private void autoSetButtonBarVisibility() {
    Button primaryButton = getPrimaryButtonView();
    Button secondaryButton = getSecondaryButtonView();
    boolean primaryVisible = primaryButton != null && primaryButton.getVisibility() == View.VISIBLE;
    boolean secondaryVisible =
        secondaryButton != null && secondaryButton.getVisibility() == View.VISIBLE;

    if (buttonContainer != null) {
      buttonContainer.setVisibility(
          primaryVisible || secondaryVisible
              ? View.VISIBLE
              : removeFooterBarWhenEmpty ? View.GONE : View.INVISIBLE);
    }
  }

10.RecoverySystem.java

10.1.rebootWipeUserData

   public static void rebootWipeUserData(Context context, boolean shutdown, String reason,
            boolean force, boolean wipeEuicc) throws IOException {
        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
        if (!force && um.hasUserRestriction(UserManager.DISALLOW_FACTORY_RESET)) {
            throw new SecurityException("Wiping data is not allowed for this user.");
        }
        final ConditionVariable condition = new ConditionVariable();
        //发送clear广播
        Intent intent = new Intent("android.intent.action.MASTER_CLEAR_NOTIFICATION");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND
                | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        context.sendOrderedBroadcastAsUser(intent, UserHandle.SYSTEM,
                android.Manifest.permission.MASTER_CLEAR,
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        condition.open();
                    }
                }, null, 0, null, null);

        // Block until the ordered broadcast has completed.
        condition.block();

        //..
        String shutdownArg = null;
        if (shutdown) {
            shutdownArg = "--shutdown_after";
        }

        String reasonArg = null;
        if (!TextUtils.isEmpty(reason)) {
            String timeStamp = DateFormat.format("yyyy-MM-ddTHH:mm:ssZ", System.currentTimeMillis()).toString();
            reasonArg = "--reason=" + sanitizeArg(reason + "," + timeStamp);
        }

        final String localeArg = "--locale=" + Locale.getDefault().toLanguageTag() ;
        bootCommand(context, shutdownArg, "--wipe_data", reasonArg, localeArg);
    }

>1.bootCommand

    private static void bootCommand(Context context, String... args) throws IOException {
        StringBuilder command = new StringBuilder();
        for (String arg : args) {
            if (!TextUtils.isEmpty(arg)) {
                command.append(arg);
                command.append("\n");
            }
        }

        // Write the command into BCB (bootloader control block) and boot from
        // there. Will not return unless failed.
        RecoverySystem rs = (RecoverySystem) context.getSystemService(Context.RECOVERY_SERVICE);
        rs.rebootRecoveryWithCommand(command.toString());

        throw new IOException("Reboot failed (no permissions?)");
    }

//

    private void rebootRecoveryWithCommand(String command) {
        try {//见11.1
            mService.rebootRecoveryWithCommand(command);
        } catch (RemoteException ignored) {
        }
    }

10.x日志

18:11:13.616 init  starting service 'setup-bcb'...
18:11:13.617 init  Created socket '/dev/socket/uncrypt', mode 600, user 1000, group 1000
18:11:13.631 init  Control message: Processed ctl.start for 'setup-bcb' from pid: 1993 (system_server)
18:11:13.723 uncrypt  [libfs_mgr]dt_fstab: Skip disabled entry for partition vendor

18:11:14.640 uncrypt    received command: [--wipe_data
                                       --reason=MainClearConfirm,2024-04-12T18:11:13Z
                                       --locale=en-GB
                                       ] (74)
18:11:14.642 uncrypt    [libfs_mgr]dt_fstab: Skip disabled entry for partition vendor
## 下边是11.2方法末尾status=100的日志
18:11:14.655 RecoverySystemService     uncrypt setup bcb successfully finished.
18:11:14.655 uncrypt    received 0, exiting now

11.RecoverySystemService

public class RecoverySystemService extends IRecoverySystem.Stub implements RebootEscrowListener {

11.1.rebootRecoveryWithCommand

    public void rebootRecoveryWithCommand(String command) {
        synchronized (sRequestLock) {
            //发送命令,见11.2
            if (!setupOrClearBcb(true, command)) {
                return;
            }

            //命令设置完成,重启设备
            PowerManager pm = mInjector.getPowerManager();
            pm.reboot(PowerManager.REBOOT_RECOVERY);
        }
    }

11.2.setupOrClearBcb

    private boolean setupOrClearBcb(boolean isSetup, String command) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.RECOVERY, null);

        final boolean available = checkAndWaitForUncryptService();
        if (!available) {
            //Uncrypt service不可用,失败说明已经30秒了。
            return false;
        }

        if (isSetup) {
        //作用:启动名字是setup-bcb的服务,见12.1
            mInjector.systemPropertiesSet("ctl.start", "setup-bcb");
        } else {
        //作用:启动名字是clear-bcb的服务
            mInjector.systemPropertiesSet("ctl.start", "clear-bcb");
        }

        // Connect to the uncrypt service socket.见补充2
        UncryptSocket socket = mInjector.connectService();
        if (socket == null) {
            return false;
        }

        try {
            // Send the BCB commands if it's to setup BCB.
            if (isSetup) {
            //可以看到,我们的factory reset走到这里就是把命令发送给Uncrypt service
                socket.sendCommand(command);//见补充2
            }

            // Read the status from the socket.
            int status = socket.getPercentageUncrypted();

            socket.sendAck();

            if (status == 100) {
               //bcb successfully finished.
            } else {
                // Error in /system/bin/uncrypt.
                return false;
            }
        } catch (IOException e) {
            return false;
        } finally {
            socket.close();
        }

        return true;
    }

>1.checkAndWaitForUncryptService

循环30次,判断下边加个值不是running,30次以后返回false

    private boolean checkAndWaitForUncryptService() {
        for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
        // "init.svc.uncrypt"
            final String uncryptService = mInjector.systemPropertiesGet(INIT_SERVICE_UNCRYPT);
            // "init.svc.setup-bcb"
            final String setupBcbService = mInjector.systemPropertiesGet(INIT_SERVICE_SETUP_BCB);
            // "init.svc.clear-bcb"
            final String clearBcbService = mInjector.systemPropertiesGet(INIT_SERVICE_CLEAR_BCB);
            final boolean busy = "running".equals(uncryptService)
                    || "running".equals(setupBcbService) || "running".equals(clearBcbService);
            if (!busy) {
            //3个都非running,说明可用,返回true
                return true;
            }
            //有running值,等待1s
            try {
                mInjector.threadSleep(1000);
            } 
        }
        //尝试30次,失败
        return false;
    }

>2.UncryptSocket

可以看到,用的是LocalSocket,

    // The socket at /dev/socket/uncrypt to communicate with uncrypt.
    private static final String UNCRYPT_SOCKET = "uncrypt";
    
        public boolean connectService() {
            mLocalSocket = new LocalSocket();
                try {
                    mLocalSocket.connect(new LocalSocketAddress(UNCRYPT_SOCKET,
                            LocalSocketAddress.Namespace.RESERVED));
                    done = true;
                    break;
                }
//..
            try {
                mInputStream = new DataInputStream(mLocalSocket.getInputStream());
                mOutputStream = new DataOutputStream(mLocalSocket.getOutputStream());
            }

//发送命令,就是把数据写入
        public void sendCommand(String command) throws IOException {
            byte[] cmdUtf8 = command.getBytes(StandardCharsets.UTF_8);
            mOutputStream.writeInt(cmdUtf8.length);
            mOutputStream.write(cmdUtf8, 0, cmdUtf8.length);
        }
//读取结果
        public int getPercentageUncrypted() throws IOException {
            return mInputStream.readInt();
        }

12.服务端

上边看到命令是通过LocalSocket发出去了,那么服务端在哪里?

12.1.uncrypt.rc

rc语法

  • 这里以setup-bcb说明
  • 服务 setup-bcb,文件路径/system/bin/uncrypt 参数 --setup-bcb
  • class 指明服务属于main这个类
  • oneshot 服务退出后,不会重新启动,否则会自动重启
  • disabled 表示服务在所在的class(main)启动后,不会自动启动。要用start server_name 或 property_set("ctl.start", server_name);才能启动。见11.2,我们是后者,修改系统属性"ctl.start"
  • socket这行表示 创建 /dev/socket/uncrypt 的socket,并把fd传递给本服务进程,socket的类型是stream,文件权限是600,后边2个system对应的是user和group的权限
# bootable/recovery/uncrypt/uncrypt.rc
service uncrypt /system/bin/uncrypt
    class main
    socket uncrypt stream 600 system system
    disabled
    oneshot

service setup-bcb /system/bin/uncrypt --setup-bcb
    class main
    socket uncrypt stream 600 system system
    disabled
    oneshot

service clear-bcb /system/bin/uncrypt --clear-bcb
    class main
    socket uncrypt stream 600 system system
    disabled
    oneshot

12.2.uncrypt.cpp

12.1里可以看到不同的action传递的参数不同,而在main方法里,会解析参数,进行不同的action操作

>1.main

int main(int argc, char** argv) {
    enum { UNCRYPT, SETUP_BCB, CLEAR_BCB, UNCRYPT_DEBUG } action;
    const char* input_path = nullptr;
    const char* map_file = CACHE_BLOCK_MAP.c_str();

    if (argc == 2 && strcmp(argv[1], "--clear-bcb") == 0) {
        action = CLEAR_BCB;
    } else if (argc == 2 && strcmp(argv[1], "--setup-bcb") == 0) {
        action = SETUP_BCB;
    } else if (argc == 1) {
        action = UNCRYPT;
    } 
//...
    android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC));
//..
    bool success = false;
    switch (action) {
        case UNCRYPT:
            success = uncrypt_wrapper(input_path, map_file, socket_fd);
            break;
        case SETUP_BCB:
            success = setup_bcb(socket_fd);
            break;
        case CLEAR_BCB:
            success = clear_bcb(socket_fd);
            break;
        default:  // Should never happen.
            LOG(ERROR) << "Invalid uncrypt action code: " << action;
            return 1;
    }

>2.流程图

  • 调用者通过修改系统属性"ctl.start"触发服务的启动 a1到b2,服务启动以后会阻塞,也就是停留在c3
  • 调用者发送4字节的数字告诉命令的长度(a4操作),c5收到
  • a6发送命令,c7接收命令,完事c8处理,c10告诉处理结果,发送100表示成功,-1表示失败
  • a11调用者获取服务端通过c10返回状态码,根据状态码做对应的处理,完事执行a12发送ack,就是写入一个0,告诉服务端结束, 服务端收到这个ack,就会走C13,也就是销毁退出socket image.png

>3.setup_bcb

static bool setup_bcb(const int socket) {
    // c5. receive message length
    int length;
    if (!android::base::ReadFully(socket, &length, 4)) {
     //读取字符串长度失败
        return false;
    }
    length = ntohl(length);

    // c7. receive message
    std::string content;
    content.resize(length);
    if (!android::base::ReadFully(socket, &content[0], length)) {
      //读取字符串失败
        return false;
    }
    LOG(INFO) << "  received command: [" << content << "] (" << content.size() << ")";
    //把读取的字符串按照换行符分成数组
    std::vector<std::string> options = android::base::Split(content, "\n");

    // c8. setup the bcb command
    std::string err;
    if (!write_bootloader_message(options, &err)) {
        LOG(ERROR) << "failed to set bootloader message: " << err;
        write_status_to_socket(-1, socket);
        return false;
    }
    if (!wipe_package.empty() && !write_wipe_package(wipe_package, &err)) {
        PLOG(ERROR) << "failed to set wipe package: " << err;
        write_status_to_socket(-1, socket);
        return false;
    }
    // c10. send "100" status
    write_status_to_socket(100, socket);
    return true;
}

>4.读取的字符串如下

received command: [--wipe_data
               --reason=MainClearConfirm,2024-04-12T18:11:13Z
               --locale=en-GB
               ] (74)

12.3.bootloader_message.cpp

>1.write_bootloader_message

bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
  bootloader_message boot = {};//结构体见补充3
  update_bootloader_message_in_struct(&boot, options);//见补充2

  return write_bootloader_message(boot, err);//见补充4
}

>2.update_bootloader_message_in_struct

  • 给结构体boot的变量command赋值"boot-recovery"
  • 给结构体boot的变量recovery赋值"recovery\n",后边再拼接上我们传递过来的命令,见12.2.4
bool update_bootloader_message_in_struct(bootloader_message* boot,
                                         const std::vector<std::string>& options) {
  if (!boot) return false;
  // Replace the command & recovery fields.
  memset(boot->command, 0, sizeof(boot->command));
  memset(boot->recovery, 0, sizeof(boot->recovery));
    //command变量赋值
  strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
  //recovery变量赋值
  strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
  for (const auto& s : options) {
  //循环在recovery变量后追加值
    strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
    if (s.back() != '\n') {
    //字符串的末尾没有换行符,追加换行符
      strlcat(boot->recovery, "\n", sizeof(boot->recovery));
    }
  }
  return true;
}

>3.struct bootloader_message

这个是bootloader_message.h里定义的结构体

struct bootloader_message {
    char command[32];
    char status[32];
    char recovery[768];

    // The 'recovery' field used to be 1024 bytes.  It has only ever
    // been used to store the recovery command line, so 768 bytes
    // should be plenty.  We carve off the last 256 bytes to store the
    // stage string (for multistage packages) and possible future
    // expansion.
    char stage[32];

    // The 'reserved' field used to be 224 bytes when it was initially
    // carved off from the 1024-byte recovery field. Bump it up to
    // 1184-byte so that the entire bootloader_message struct rounds up
    // to 2048-byte.
    char reserved[1184];
};

>4.write_bootloader_message

通过get_misc_blk_device获取一个路径,数据最终是写到这里了,具体就不看了,不会C,看起来太费劲。

bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
  std::string misc_blk_device = get_misc_blk_device(err);
  if (misc_blk_device.empty()) {
    return false;
  }
  return write_bootloader_message_to(boot, misc_blk_device, err);
}
bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
                                 std::string* err) {
  return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
                              BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
}
static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
                                 size_t offset, std::string* err) {
  android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
  if (fd == -1) {
    *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
                                       strerror(errno));
    return false;
  }
  if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
    *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
                                       strerror(errno));
    return false;
  }
  if (!android::base::WriteFully(fd, p, size)) {
    *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
                                       strerror(errno));
    return false;
  }
  if (fsync(fd) == -1) {
    *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
                                       strerror(errno));
    return false;
  }
  return true;
}