Android8.1 Settings中清除数据流程分析

1、首先我们根据关键词"清除数据"去找,会找到如下文件

packages/apps/Settings/src/com/android/settings/applications/AppStorageSettings.java
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mCacheCleared = savedInstanceState.getBoolean(KEY_CACHE_CLEARED, false);
            mDataCleared = savedInstanceState.getBoolean(KEY_DATA_CLEARED, false);
            mCacheCleared = mCacheCleared || mDataCleared;
        }

        addPreferencesFromResource(R.xml.app_storage_settings);
        setupViews();
        initMoveDialog();
        if (SUPPORT_SUPER_POWER_SAVE) {
            mPowerManagerEx = IPowerManagerEx.Stub.asInterface(ServiceManager.getService("power_ex"));
        }
    }

2、根据对应的app_storage_settings.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:settings="http://schemas.android.com/apk/res-auto"
                  android:title="@string/application_info_label">
	//这个就是对应的清除数据的布局处理
    <com.android.settings.applications.LayoutPreference
        android:key="header_view"
        android:layout="@layout/app_action_buttons"
        android:selectable="false" />

    <com.android.settings.applications.SpacePreference
        android:key="storage_space"
        android:layout_height="8dp" />

    <Preference
        android:key="storage_used"
        android:title="@string/storage_used"
        android:summary="@string/storage_type_internal"
        android:selectable="false"
        android:layout="@layout/horizontal_preference" />

    <com.android.settings.applications.LayoutPreference
        android:key="change_storage_button"
        android:selectable="false"
        android:layout="@layout/single_button_panel" />

    <PreferenceCategory
        android:key="storage_category"
        android:title="@string/app_info_storage_title"
        settings:allowDividerAbove="false"
        settings:allowDividerBelow="false">

        <Preference
            android:key="app_size"
            android:title="@string/application_size_label"
            android:selectable="false"
            android:layout="@layout/horizontal_preference" />

        <Preference
            android:key="data_size"
            android:title="@string/data_size_label"
            android:selectable="false"
            android:layout="@layout/horizontal_preference" />

        <Preference
            android:key="cache_size"
            android:title="@string/cache_size_label"
            android:selectable="false"
            android:layout="@layout/horizontal_preference" />

        <Preference
            android:key="total_size"
            android:title="@string/total_size_label"
            android:selectable="false"
            android:layout="@layout/horizontal_preference" />

        <com.android.settings.applications.SpacePreference
            android:layout_height="8dp" />

    </PreferenceCategory>

    <PreferenceCategory
        android:key="uri_category"
        android:layout="@layout/headerless_preference_category"
        settings:allowDividerAbove="false"
        settings:allowDividerBelow="false">
        <com.android.settings.applications.LayoutPreference
            android:key="clear_uri_button"
            android:layout="@layout/single_button_panel"
            android:selectable="false" />
    </PreferenceCategory>

</PreferenceScreen>

3、清除数据对应的布局如下

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingTop="8dp"
    android:paddingStart="8dp"
    android:paddingEnd="8dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/left_button"
        style="@style/AppActionPrimaryButton"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp" />

    <Button
        android:id="@+id/right_button"
        style="@style/AppActionPrimaryButton"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp" />

</LinearLayout>

4、找到对应Button的点击事件,如下处理

 @Override
    public void onClick(View v) {
        /* Bug 914729 :  Crash occured if clear data of some app on Ultra Saving mode. @{*/
        boolean onUltraSavingModeShowDialogInner = false;
        if (isInUltraSavingMode() && !isAllowedAppInUltraSavingMode(mAppEntry.info.packageName)) {
            onUltraSavingModeShowDialogInner = true;
        }
        /*@}*/
        if (v == mClearCacheButton) {
            if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
                RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
                        getActivity(), mAppsControlDisallowedAdmin);
                return;
            } else if (mClearCacheObserver == null) { // Lazy initialization of observer
                mClearCacheObserver = new ClearCacheObserver();
            }
            mMetricsFeatureProvider.action(getContext(),
                    MetricsEvent.ACTION_SETTINGS_CLEAR_APP_CACHE);
            /* Modify to delete application cache files of appclone app {@ */
            if (mUserId > 0 && AppCloneUserInfo.isAppCloneUserId(mUserId)) {
                mPm.deleteApplicationCacheFilesAsUser(mPackageName, mUserId, mClearCacheObserver);
            } else {
                mPm.deleteApplicationCacheFiles(mPackageName, mClearCacheObserver);
            }
            /* @} */
        } else if (v == mClearDataButton) {//这里就是清除缓存的处理
            if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
                RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
                        getActivity(), mAppsControlDisallowedAdmin);
            } else if (mAppEntry.info.manageSpaceActivityName != null && !onUltraSavingModeShowDialogInner) {
                if (!Utils.isMonkeyRunning()) {
                    Intent intent = new Intent(Intent.ACTION_DEFAULT);
                    intent.setClassName(mAppEntry.info.packageName,
                            mAppEntry.info.manageSpaceActivityName);
                    startActivityForResult(intent, REQUEST_MANAGE_SPACE);
                }
            } else {
                showDialogInner(DLG_CLEAR_DATA, 0);
            }
        } else if (v == mChangeStorageButton && mDialogBuilder != null && !isMoveInProgress()) {
            mDialogBuilder.show();
        } else if (v == mClearUriButton) {
            if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
                RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
                        getActivity(), mAppsControlDisallowedAdmin);
            } else {
                clearUriPermissions();
            }
        }
    }

5,点击清除缓存按钮后会弹出如下对话框的逻辑处理

showDialogInner(DLG_CLEAR_DATA, 0);
 protected void showDialogInner(int id, int moveErrorCode) {
        DialogFragment newFragment = MyAlertDialogFragment.newInstance(id, moveErrorCode);
        newFragment.setTargetFragment(this, 0);
        newFragment.showAllowingStateLoss(getFragmentManager(), "dialog " + id);
    }

6、最终清除缓存处理逻辑如下

 case DLG_CLEAR_DATA:
                return new AlertDialog.Builder(getActivity())
                        .setTitle(getActivity().getText(R.string.clear_data_dlg_title))
                        .setMessage(getActivity().getText(R.string.clear_data_dlg_text))
                        .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                // Clear user data here
                                initiateClearUserData();
                            }
                        })
                        .setNegativeButton(R.string.dlg_cancel, null)
                        .create();
//

7、如下是处理清除缓存

packages/apps/Settings/src/com/android/settings/applications/AppStorageSettings.java
 private void initiateClearUserData() {
        mMetricsFeatureProvider.action(getContext(), MetricsEvent.ACTION_SETTINGS_CLEAR_APP_DATA);
        mClearDataButton.setEnabled(false);
        // Invoke uninstall or clear user data based on sysPackage
        String packageName = mAppEntry.info.packageName;
        Log.i(TAG, "Clearing user data for package : " + packageName);
        if (mClearDataObserver == null) {
            mClearDataObserver = new ClearUserDataObserver();
        }
        /* Modify to clear application user data of appclone app {@ */
        boolean res = false;
        if (mUserId > 0 && AppCloneUserInfo.isAppCloneUserId(mUserId)) {
            try {
                res = ActivityManagerNative.getDefault().clearApplicationUserData(packageName,
                        mClearDataObserver, mUserId);
            } catch (RemoteException e) {
                Log.e(TAG, "clearApplicationUserData :" + e);
            }
        } else {
            ActivityManager am = (ActivityManager)
                getActivity().getSystemService(Context.ACTIVITY_SERVICE);
            res = am.clearApplicationUserData(packageName, mClearDataObserver);
        }
        /* @} */
        if (!res) {
            // Clearing data failed for some obscure reason. Just log error for now
            Log.i(TAG, "Couldnt clear application user data for package:"+packageName);
            showDialogInner(DLG_CANNOT_CLEAR_DATA, 0);
        } else {
            mClearDataButton.setText(R.string.recompute_size);
        }
    }

8、打印结果如下

01-01 11:34:08.743  2810  2810 D AppStorageSettings: mClearDataButton!
01-01 11:34:10.231  2810  2810 I AppStorageSettings: Clearing user data for package : com.zqc.myapplication
01-01 11:34:10.231  2810  2810 I AppStorageSettings: mUserId : 0
01-01 11:34:10.231  2810  2810 I AppStorageSettings: AppCloneUserInfo.isAppCloneUserId(mUserId) : false
01-01 11:34:10.244  2810  2810 I AppStorageSettings: res : true
01-01 11:34:10.528  2810  2810 I AppStorageSettings: Cleared user data for package : com.zqc.myapplication

9、实际执行代码

 ActivityManager am = (ActivityManager)
                getActivity().getSystemService(Context.ACTIVITY_SERVICE);
            res = am.clearApplicationUserData(packageName, mClearDataObserver);

你可能感兴趣的:(工作记录)