1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.settings.users; 18 19 import android.app.ActivityManagerNative; 20 import android.app.IUserSwitchObserver; 21 import android.app.Service; 22 import android.content.BroadcastReceiver; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.SharedPreferences; 27 import android.content.pm.PackageManager; 28 import android.os.IBinder; 29 import android.os.IRemoteCallback; 30 import android.os.RemoteException; 31 import android.os.UserHandle; 32 import android.os.UserManager; 33 import android.util.Log; 34 35 import com.android.tv.settings.system.SecurityFragment; 36 37 public class UserSwitchListenerService extends Service { 38 39 private static final boolean DEBUG = false; 40 private static final String TAG = "RestrictedProfile"; 41 42 private static final String RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY = 43 "com.android.tv.settings.users.RestrictedProfileActivityLauncherEntry"; 44 private static final String SHARED_PREFERENCES_NAME = "RestrictedProfileSharedPreferences"; 45 private static final String 46 ON_BOOT_USER_ID_PREFERENCE = "UserSwitchOnBootBroadcastReceiver.userId"; 47 48 public static class BootReceiver extends BroadcastReceiver { 49 @Override 50 public void onReceive(final Context context, Intent intent) { 51 52 boolean isSystemUser = UserManager.get(context).isSystemUser(); 53 54 if (isSystemUser) { 55 context.startService(new Intent(context, UserSwitchListenerService.class)); 56 int bootUserId = getBootUser(context); 57 if (DEBUG) { 58 Log.d(TAG, 59 "boot completed, user is " + UserHandle.myUserId() 60 + " boot user id: " 61 + bootUserId); 62 } 63 if (UserHandle.myUserId() != bootUserId) { 64 switchUserNow(bootUserId); 65 } 66 } 67 68 updateLaunchPoint(context, null != SecurityFragment.findRestrictedUser( 69 (UserManager) context.getSystemService(Context.USER_SERVICE))); 70 } 71 } 72 73 public static void updateLaunchPoint(Context context, boolean enableLaunchPoint) { 74 if (DEBUG) { 75 Log.d(TAG, "updating launch point: " + enableLaunchPoint); 76 } 77 78 PackageManager pm = context.getPackageManager(); 79 ComponentName compName = new ComponentName(context, 80 RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY); 81 pm.setComponentEnabledSetting(compName, 82 enableLaunchPoint ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED 83 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 84 PackageManager.DONT_KILL_APP); 85 } 86 87 static void setBootUser(Context context, int userId) { 88 SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 89 Context.MODE_PRIVATE).edit(); 90 editor.putInt(ON_BOOT_USER_ID_PREFERENCE, userId); 91 editor.apply(); 92 } 93 94 static int getBootUser(Context context) { 95 SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, 96 Context.MODE_PRIVATE); 97 return prefs.getInt(ON_BOOT_USER_ID_PREFERENCE, UserHandle.USER_SYSTEM); 98 } 99 100 private static void switchUserNow(int userId) { 101 try { 102 ActivityManagerNative.getDefault().switchUser(userId); 103 } catch (RemoteException re) { 104 Log.e(TAG, "Caught exception while switching user! ", re); 105 } 106 } 107 108 @Override 109 public void onCreate() { 110 super.onCreate(); 111 try { 112 ActivityManagerNative.getDefault().registerUserSwitchObserver( 113 new IUserSwitchObserver.Stub() { 114 @Override 115 public void onUserSwitching(int newUserId, IRemoteCallback reply) { 116 } 117 118 @Override 119 public void onUserSwitchComplete(int newUserId) throws RemoteException { 120 if (DEBUG) { 121 Log.d(TAG, "user has been foregrounded: " + newUserId); 122 } 123 setBootUser(UserSwitchListenerService.this, newUserId); 124 } 125 126 @Override 127 public void onForegroundProfileSwitch(int profileId) 128 throws RemoteException { 129 } 130 }); 131 } catch (RemoteException e) { 132 } 133 } 134 135 @Override 136 public int onStartCommand(Intent intent, int flags, int startId) { 137 return START_STICKY; 138 } 139 140 @Override 141 public IBinder onBind(Intent intent) { 142 return null; 143 } 144 } 145