1 /*
2  * Copyright (C) 2015 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.settings.dashboard.profileselector;
18 
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.DialogInterface.OnClickListener;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.util.Log;
28 
29 import androidx.appcompat.app.AlertDialog;
30 import androidx.fragment.app.DialogFragment;
31 import androidx.fragment.app.FragmentManager;
32 
33 import com.android.settingslib.drawer.Tile;
34 
35 import java.util.List;
36 
37 public class ProfileSelectDialog extends DialogFragment implements OnClickListener {
38 
39     private static final String TAG = "ProfileSelectDialog";
40     private static final String ARG_SELECTED_TILE = "selectedTile";
41     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
42 
43     private Tile mSelectedTile;
44 
show(FragmentManager manager, Tile tile)45     public static void show(FragmentManager manager, Tile tile) {
46         ProfileSelectDialog dialog = new ProfileSelectDialog();
47         Bundle args = new Bundle();
48         args.putParcelable(ARG_SELECTED_TILE, tile);
49         dialog.setArguments(args);
50         dialog.show(manager, "select_profile");
51     }
52 
53     @Override
onCreate(Bundle savedInstanceState)54     public void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         mSelectedTile = getArguments().getParcelable(ARG_SELECTED_TILE);
57     }
58 
59     @Override
onCreateDialog(Bundle savedInstanceState)60     public Dialog onCreateDialog(Bundle savedInstanceState) {
61         Context context = getActivity();
62         AlertDialog.Builder builder = new AlertDialog.Builder(context);
63         UserAdapter adapter = UserAdapter.createUserAdapter(UserManager.get(context), context,
64                 mSelectedTile.userHandle);
65         builder.setTitle(com.android.settingslib.R.string.choose_profile)
66                 .setAdapter(adapter, this);
67 
68         return builder.create();
69     }
70 
71     @Override
onClick(DialogInterface dialog, int which)72     public void onClick(DialogInterface dialog, int which) {
73         UserHandle user = mSelectedTile.userHandle.get(which);
74         // Show menu on top level items.
75         final Intent intent = mSelectedTile.getIntent();
76         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
77         getActivity().startActivityAsUser(intent, user);
78     }
79 
updateUserHandlesIfNeeded(Context context, Tile tile)80     public static void updateUserHandlesIfNeeded(Context context, Tile tile) {
81         List<UserHandle> userHandles = tile.userHandle;
82         if (tile.userHandle == null || tile.userHandle.size() <= 1) {
83             return;
84         }
85         final UserManager userManager = UserManager.get(context);
86         for (int i = userHandles.size() - 1; i >= 0; i--) {
87             if (userManager.getUserInfo(userHandles.get(i).getIdentifier()) == null) {
88                 if (DEBUG) {
89                     Log.d(TAG, "Delete the user: " + userHandles.get(i).getIdentifier());
90                 }
91                 userHandles.remove(i);
92             }
93         }
94     }
95 }
96