Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89ec26c3 authored by haibiao.lu's avatar haibiao.lu Committed by Rohit Sekhar
Browse files

[FPS-2941] Set stock launcher as default home app at the end of SUW[1/2]

* Root Cause
  new feature
* Solution
  Fix code to meet customer's requestment
* Test Steps
  frameworks

Change-Id: I6cc68336f7050c41d2c4a7a4f71dc0300f3c40e4
(cherry picked from commit 6b10857721fbf29b5ed95770b0a3867d6e8d20ef)
parent aa4d159a
Loading
Loading
Loading
Loading
+313 −0
Original line number Diff line number Diff line
package com.android.server.pm;

import java.util.Arrays;
import java.util.ArrayList;

import android.os.UserHandle;
import android.os.PersistableBundle;
import android.os.ServiceManager;
import android.os.Handler;
import android.content.Context;

import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.IntentFilter;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.telephony.CarrierConfigManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.os.Process;
import android.os.SystemProperties;
import android.content.ComponentName;


public class AppStateController {

    private final String TAG = "AppStateController";

    private static final String KEY_CARRIER_PREINSTALL = "carrier_preinstall";

    private static final String CARRIER_PREINSTALL_ARRAY[] = {"de.telekom.tsc","com.aura.oobe.deutsche", "com.orange.aura.oobe", "com.orange.update","com.gohappy.mobileapp","com.fetself","com.omusic.gPhone","net.fetnet.fetvod","com.fet.fridaywallet"};

    private Context mContext;

    private Handler mHandler;

    private ArrayList<AppState> mAppStateArrayList = new ArrayList();

    private boolean mHasSetAppState;

    private boolean hasSimStateChanged = false;

    private boolean mIsCarrierConfigLoaded = false;

    private boolean mIsCarrierConfigReveiver = false;
    private static final String DSDLOCKED_CARRIERID = "persist.radio.dsd.locked";
    private static final String DSDLOCKED_HASENABLEAPP = "persist.sys.dsd.enableapp";
    private static final String DSDLOCKED_REBOOT = "persist.radio.dsd.locked.reboot";

    private int mUserId;

    public static final String FILES_TARGET_PACKAGE = "com.google.android.documentsui";
    public static final String FILES_TARGET_CLASS = "com.android.documentsui.LauncherActivity";

    private final PackageManagerService mPm;

    /**
     * @param context
     * @param handler
     * @hide
     */
    public AppStateController(PackageManagerService pm,Context context, Handler handler) {
        this.mContext = context;
        this.mHandler = handler;
        mPm = pm;
        mUserId = Process.myUserHandle().myUserId();
        registerReceiver();
        initCarrierAppList();
        boolean islock = SystemProperties.getBoolean(DSDLOCKED_CARRIERID, false);
        if(!islock){
            setPreInstallCarrierApkState();
        }
        Log.d(TAG, "AppStateController init");
    }

    private void initCarrierAppList() {
        for (String pkg : CARRIER_PREINSTALL_ARRAY) {
            AppState appState = new AppState();
            appState.installState = false;
            appState.pkgName = pkg;
            mAppStateArrayList.add(appState);
        }
    }

    private void updateCarrierAppState() {
        CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
        //PersistableBundle config = configManager.getConfig();
        PersistableBundle config = configManager.getConfigLocked();
        if (config != null) {
            Log.d(TAG, "config != null");
            String[] preInstallApps = config.getStringArray(KEY_CARRIER_PREINSTALL);
            if (preInstallApps != null && preInstallApps.length > 0) { //dsd lock carrier_install is not null
                Log.d(TAG, "get carrier config preintall count = " + preInstallApps.length);
                for (String app : preInstallApps) {
                    Log.d(TAG, "get carrier config preintall " + app);
                    for (AppState appState : mAppStateArrayList) {
                        if (appState.pkgName.equals(app)) {
                            appState.installState = true;
                            if (!mIsCarrierConfigLoaded) {
                                Log.d(TAG, "mIsCarrierConfigLoaded = true");
                                mIsCarrierConfigLoaded = true;
                            }
                        }
                    }
                }
            } else {  //dsd lock carrier_install is null

                config = configManager.getConfig();
                preInstallApps = config.getStringArray(KEY_CARRIER_PREINSTALL);

                if (preInstallApps != null && preInstallApps.length > 0) { //dsd lock carrier_install is not null
                    Log.d(TAG, "else get carrier config preintall count = " + preInstallApps.length);
                    if("com.gohappy.mobileapp".equals(preInstallApps[0])){
                        for (String app : preInstallApps) {
                            Log.d(TAG, "else get carrier config preintall " + app);
                            for (AppState appState : mAppStateArrayList) {
                                if (appState.pkgName.equals(app)) {
                                    appState.installState = true;
                                    if (!mIsCarrierConfigLoaded) {
                                        Log.d(TAG, "mIsCarrierConfigLoaded = true");
                                        mIsCarrierConfigLoaded = true;
                                    }
                                }
                            }
                        }
                    }

                }

            }
        }
    }

    private void registerReceiver() {
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED);
        //mIntentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
        mIntentFilter.addAction(Intent.ACTION_LOCKED_BOOT_COMPLETED);
        mIntentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
        mIntentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
        mIntentFilter.addAction(Intent.ACTION_USER_SWITCHED);
        mIntentFilter.addAction(Intent.ACTION_USER_REMOVED);
        mIntentFilter.addAction(Intent.ACTION_USER_ADDED);
        mContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
                    int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
                    Log.d(TAG,"ACTION_USER_SWITCHED userId = " + userId);
                    Log.d(TAG,"ACTION_USER_SWITCHED getActiveLauncherPackageName = " + mPm.getActiveLauncherPackageName(userId));
                    if(mPm.getActiveLauncherPackageName(userId) == null || 
                       "com.google.android.setupwizard".equals(mPm.getActiveLauncherPackageName(userId))){
                        mPm.setActiveLauncherPackage("com.android.launcher3",userId,successful -> {
                            if (successful) {
                                 Log.d(TAG,"default launcher set success for userId " + userId);
                            }else{
                                 Log.d(TAG,"default launcher set fail for userId " + userId);
                            }
                        });
                    }
                    if (userId != mUserId) {
                        Log.d(TAG, "ACTION_USER_SWITCHED" + " old user id = " + userId + " new user id = " + mUserId);
                        mUserId = userId;
                        updateCarrierAppState();
                        setPreInstallCarrierApkState();
                    }

                }

                if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) {
                    int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
                    Log.d(TAG,"ACTION_USER_ADDED userId = " + userId);
                    Log.d(TAG,"ACTION_USER_ADDED getActiveLauncherPackageName = " + mPm.getActiveLauncherPackageName(userId));
                    if(mPm.getActiveLauncherPackageName(userId) == null || 
                       "com.google.android.setupwizard".equals(mPm.getActiveLauncherPackageName(userId))){
                        mPm.setActiveLauncherPackage("com.android.launcher3",userId,successful -> {
                            if (successful) {
                                 Log.d(TAG,"default launcher set success for userId " + userId);
                            }else{
                                 Log.d(TAG,"default launcher set fail for userId " + userId);
                            }
                        });
                    }
                    if (userId != UserHandle.USER_SYSTEM){
                        disableDocumentUI(userId);
                    }
                    if (userId != mUserId) {
                        Log.d(TAG, "ACTION_USER_ADDED" + " old user id = " + userId + " new user id = " + mUserId);
                        mUserId = userId;
                        updateCarrierAppState();
                        setPreInstallCarrierApkState_forOtherUser();
                    }
                }

                if (TelephonyManager.ACTION_SIM_APPLICATION_STATE_CHANGED.equals(intent.getAction())) {
                    hasSimStateChanged = true;
                    int simStatus = intent.getIntExtra(TelephonyManager.EXTRA_SIM_STATE, -99);
                    Log.d(TAG, "ACTION_SIM_APPLICATION_STATE_CHANGED" + " simStatus= " + simStatus);
                }
                //if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) && !hasSimStateChanged){
                if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
                    Log.d(TAG, "LOCKED_BOOT_COMPLETED");
//                    if (!mHasSetAppState && mIsCarrierConfigReveiver) {
//                        setPreInstallCarrierApkState();
//                        mHasSetAppState = true;
//                    }
                }

                if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
                    Log.d(TAG, "ACTION_BOOT_COMPLETED");
                    int[] userIds = UserManagerService.getInstance().getUserIdsIncludingPreCreated();
                    for (int uid : userIds){
                        if (uid != UserHandle.USER_SYSTEM){
                            mUserId = uid;
                            Log.d(TAG, "disable preload app on user " + mUserId);
                            updateCarrierAppState();
                            setPreInstallCarrierApkState_forOtherUser();
                            mUserId = 0;
                        }
                    }
                }

                if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) {
                    Log.d(TAG, "ACTION_CARRIER_CONFIG_CHANGED");
                    updateCarrierAppState();
                    mHandler.postDelayed(() -> judgeAndFireSetAppState(),200);

                }
            }
        }, mIntentFilter);
    }

    private void disableDocumentUI(int userId){
        final ComponentName component = new ComponentName(FILES_TARGET_PACKAGE, FILES_TARGET_CLASS);
        IPackageManager mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        try{
            mIPm.setComponentEnabledSetting(component,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP, userId, mContext.getBasePackageName());
        } catch (Exception e) {
            Log.d(TAG, " disableDocumentUI error " + e.getMessage());
        }
    }

    private void judgeAndFireSetAppState() {
        mHasSetAppState = SystemProperties.getBoolean(DSDLOCKED_HASENABLEAPP, false);
        boolean rebootafterfirstdsdlock  = SystemProperties.getBoolean(DSDLOCKED_REBOOT, false);
        if (!mIsCarrierConfigLoaded || mHasSetAppState || !rebootafterfirstdsdlock) {
            Log.d(TAG, " judgeAndFireSetAppState mIsCarrierConfigLoaded  " + mIsCarrierConfigLoaded + " mHasSetAppState = " + mHasSetAppState);
            return;
        }
        setPreInstallCarrierApkState();
        mHasSetAppState = true;
        SystemProperties.set(DSDLOCKED_HASENABLEAPP, "1");
    }

    private void setPreInstallCarrierApkState() {
        IPackageManager mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        for (AppState appState : mAppStateArrayList) {
            updateInstallState(appState.pkgName, appState.installState, mIPm);
            if("com.orange.update".equals(appState.pkgName) && appState.installState){
                updateInstallState("com.fairphone.gallery", false, mIPm);
            }
        }
    }

    private void updateInstallState(String pkg, boolean isSimAppropriate, IPackageManager ipm) {
        Log.d(TAG, pkg + " updateInstallState sim Appropriate  " + isSimAppropriate + " mUserId = " + mUserId);
        try {
           boolean success = ipm.setSystemAppInstallState(pkg, isSimAppropriate, mUserId);
            Log.d(TAG, pkg +" updateInstallState:" + success);
            if (!isSimAppropriate){
                int[] userIds = UserManagerService.getInstance().getUserIdsIncludingPreCreated();
                for (int uid : userIds){
                    if (uid != UserHandle.USER_SYSTEM){
                        ipm.setApplicationHiddenSettingAsUser(pkg,true,uid);
                        ipm.setApplicationEnabledSetting(pkg,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.UNINSTALL_REASON_UNKNOWN,uid,mContext.getBasePackageName());
                    }
               }

            }
        } catch (Exception e) {
            Log.d(TAG, pkg + " updateInstallState error " + e.getMessage());
        }

    }

    private void setPreInstallCarrierApkState_forOtherUser() {
        IPackageManager mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
        for (AppState appState : mAppStateArrayList) {
            updateInstallState_forOtherUser(appState.pkgName, appState.installState, mIPm);
        }
    }

    private void updateInstallState_forOtherUser(String pkg, boolean isSimAppropriate, IPackageManager ipm) {
        try {
            if (!isSimAppropriate){
                if (mUserId != UserHandle.USER_SYSTEM){
                    Log.d(TAG, pkg + " updateInstallState_forOtherUser  " + isSimAppropriate + " mUserId = " + mUserId);
                    ipm.setApplicationEnabledSetting(pkg,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.UNINSTALL_REASON_UNKNOWN,mUserId,mContext.getBasePackageName());
                }

            }
        } catch (Exception e) {
            Log.d(TAG, pkg + " updateInstallState error " + e.getMessage());
        }

    }

    private class AppState {
        public String pkgName;
        public boolean installState;
    }

}
+14 −0
Original line number Diff line number Diff line
@@ -4208,6 +4208,18 @@ public class PackageManagerService implements PackageSender, TestUtilityService
        PackageManagerServiceUtils.enforceSystemOrRoot(
                "Only the system can claim the system is ready");

        for (int userId : UserManagerService.getInstance().getUserIds()) {
            if(getActiveLauncherPackageName(userId) == null){
                setActiveLauncherPackage("com.android.launcher3",userId,successful -> {
                    if (successful) {
                         Log.d(TAG,"default launcher set success for userId " + userId);
                    }else{
                         Log.d(TAG,"default launcher set fail for userId " + userId);
                    }
                });
            }
        }

        final ContentResolver resolver = mContext.getContentResolver();
        if (mReleaseOnSystemReady != null) {
            for (int i = mReleaseOnSystemReady.size() - 1; i >= 0; --i) {
@@ -4373,6 +4385,8 @@ public class PackageManagerService implements PackageSender, TestUtilityService
        // Prune unused static shared libraries which have been cached a period of time
        schedulePruneUnusedStaticSharedLibraries(false /* delay */);

        new AppStateController(this,mContext,mHandler);

        DexUseManagerLocal dexUseManager = DexOptHelper.getDexUseManagerLocal();
        if (dexUseManager != null) {
            dexUseManager.systemReady();