Home | History | Annotate | Download | only in users
      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.Activity;
     20 import android.app.ActivityManagerNative;
     21 import android.app.IUserSwitchObserver;
     22 import android.app.Service;
     23 import android.content.BroadcastReceiver;
     24 import android.content.ComponentName;
     25 import android.content.ContentResolver;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.IntentFilter;
     29 import android.content.SharedPreferences;
     30 import android.content.pm.PackageManager;
     31 import android.os.IBinder;
     32 import android.os.IRemoteCallback;
     33 import android.os.RemoteException;
     34 import android.os.UserHandle;
     35 import android.os.UserManager;
     36 import android.provider.Settings;
     37 import android.util.Log;
     38 
     39 public class UserSwitchListenerService extends Service {
     40 
     41     private static final boolean DEBUG = false;
     42     private static final String TAG = "RestrictedProfile";
     43 
     44     private static final String RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY =
     45             "com.android.tv.settings.users.RestrictedProfileActivityLauncherEntry";
     46     private static final String SHARED_PREFERENCES_NAME = "RestrictedProfileSharedPreferences";
     47     private static final String
     48             ON_BOOT_USER_ID_PREFERENCE = "UserSwitchOnBootBroadcastReceiver.userId";
     49 
     50     public static class BootReceiver extends BroadcastReceiver {
     51         @Override
     52         public void onReceive(final Context context, Intent intent) {
     53 
     54             boolean isOwner = UserHandle.myUserId() == UserHandle.USER_OWNER;
     55 
     56             if (isOwner) {
     57                 context.startService(new Intent(context, UserSwitchListenerService.class));
     58                 int bootUserId = getBootUser(context);
     59                 if (DEBUG) {
     60                     Log.d(TAG,
     61                             "boot completed, user is " + UserHandle.myUserId()
     62                             + " boot user id: "
     63                             + bootUserId);
     64                 }
     65                 if (UserHandle.myUserId() != bootUserId) {
     66                     RestrictedProfileActivity.switchUserNow(bootUserId);
     67                 }
     68             }
     69 
     70             updateLaunchPoint(context, null != RestrictedProfileActivity.findRestrictedUser(
     71                     (UserManager) context.getSystemService(Context.USER_SERVICE)));
     72         }
     73     }
     74 
     75     static void updateLaunchPoint(Context context, boolean enableLaunchPoint) {
     76         if (DEBUG) {
     77             Log.d(TAG, "updating launch point: " + enableLaunchPoint);
     78         }
     79 
     80         PackageManager pm = context.getPackageManager();
     81         ComponentName compName = new ComponentName(context,
     82                 RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY);
     83         pm.setComponentEnabledSetting(compName,
     84                 enableLaunchPoint ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
     85                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
     86                 PackageManager.DONT_KILL_APP);
     87     }
     88 
     89     static void setBootUser(Context context, int userId) {
     90         SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
     91                 Context.MODE_PRIVATE).edit();
     92         editor.putInt(ON_BOOT_USER_ID_PREFERENCE, userId);
     93         editor.apply();
     94     }
     95 
     96     static int getBootUser(Context context) {
     97         SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
     98                 Context.MODE_PRIVATE);
     99         return prefs.getInt(ON_BOOT_USER_ID_PREFERENCE, UserHandle.USER_OWNER);
    100     }
    101 
    102     @Override
    103     public void onCreate() {
    104         super.onCreate();
    105         try {
    106             ActivityManagerNative.getDefault().registerUserSwitchObserver(
    107                     new IUserSwitchObserver.Stub() {
    108                         @Override
    109                         public void onUserSwitching(int newUserId, IRemoteCallback reply) {
    110                         }
    111 
    112                         @Override
    113                         public void onUserSwitchComplete(int newUserId) throws RemoteException {
    114                             if (DEBUG) {
    115                                 Log.d(TAG, "user has been foregrounded: " + newUserId);
    116                             }
    117                             setBootUser(UserSwitchListenerService.this, newUserId);
    118                         }
    119                     });
    120         } catch (RemoteException e) {
    121         }
    122     }
    123 
    124     @Override
    125     public int onStartCommand(Intent intent, int flags, int startId) {
    126         return START_STICKY;
    127     }
    128 
    129     @Override
    130     public IBinder onBind(Intent intent) {
    131         return null;
    132     }
    133 }
    134