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