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.OnCancelListener;
23 import android.content.DialogInterface.OnClickListener;
24 import android.content.DialogInterface.OnDismissListener;
25 import android.content.DialogInterface.OnShowListener;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.os.UserHandle;
29 import android.os.UserManager;
30 import android.util.Log;
31 
32 import androidx.appcompat.app.AlertDialog;
33 import androidx.fragment.app.DialogFragment;
34 import androidx.fragment.app.FragmentManager;
35 
36 import com.android.settings.overlay.FeatureFactory;
37 import com.android.settingslib.drawer.Tile;
38 
39 import java.util.List;
40 
41 public class ProfileSelectDialog extends DialogFragment implements OnClickListener {
42 
43     private static final String TAG = "ProfileSelectDialog";
44     private static final String ARG_SELECTED_TILE = "selectedTile";
45     private static final String ARG_SOURCE_METRIC_CATEGORY = "sourceMetricCategory";
46     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
47 
48     private int mSourceMetricCategory;
49     private Tile mSelectedTile;
50     private OnShowListener mOnShowListener;
51     private OnCancelListener mOnCancelListener;
52     private OnDismissListener mOnDismissListener;
53 
54     /**
55      * Display the profile select dialog, adding the fragment to the given FragmentManager.
56      * @param manager The FragmentManager this fragment will be added to.
57      * @param tile The tile for this fragment.
58      * @param sourceMetricCategory The source metric category.
59      * @param onShowListener The listener listens to the dialog showing event.
60      * @param onDismissListener The listener listens to the dialog dismissing event.
61      * @param onCancelListener The listener listens to the dialog cancelling event.
62      */
show(FragmentManager manager, Tile tile, int sourceMetricCategory, OnShowListener onShowListener, OnDismissListener onDismissListener, OnCancelListener onCancelListener)63     public static void show(FragmentManager manager, Tile tile, int sourceMetricCategory,
64             OnShowListener onShowListener, OnDismissListener onDismissListener,
65             OnCancelListener onCancelListener) {
66         final ProfileSelectDialog dialog = new ProfileSelectDialog();
67         final Bundle args = new Bundle();
68         args.putParcelable(ARG_SELECTED_TILE, tile);
69         args.putInt(ARG_SOURCE_METRIC_CATEGORY, sourceMetricCategory);
70         dialog.setArguments(args);
71         dialog.mOnShowListener = onShowListener;
72         dialog.mOnDismissListener = onDismissListener;
73         dialog.mOnCancelListener = onCancelListener;
74         dialog.show(manager, "select_profile");
75     }
76 
77     @Override
onCreate(Bundle savedInstanceState)78     public void onCreate(Bundle savedInstanceState) {
79         super.onCreate(savedInstanceState);
80         mSelectedTile = getArguments().getParcelable(ARG_SELECTED_TILE);
81         mSourceMetricCategory = getArguments().getInt(ARG_SOURCE_METRIC_CATEGORY);
82     }
83 
84     @Override
onCreateDialog(Bundle savedInstanceState)85     public Dialog onCreateDialog(Bundle savedInstanceState) {
86         final Context context = getActivity();
87         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
88         final UserAdapter adapter = UserAdapter.createUserAdapter(UserManager.get(context), context,
89                 mSelectedTile.userHandle);
90         builder.setTitle(com.android.settingslib.R.string.choose_profile)
91                 .setAdapter(adapter, this);
92 
93         return builder.create();
94     }
95 
96     @Override
onClick(DialogInterface dialog, int which)97     public void onClick(DialogInterface dialog, int which) {
98         final UserHandle user = mSelectedTile.userHandle.get(which);
99         // Show menu on top level items.
100         final Intent intent = mSelectedTile.getIntent();
101         FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider()
102                 .logStartedIntentWithProfile(intent, mSourceMetricCategory,
103                         which == 1 /* isWorkProfile */);
104         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
105         getActivity().startActivityAsUser(intent, user);
106     }
107 
108     @Override
onStart()109     public void onStart() {
110         super.onStart();
111         // The fragment shows the dialog within onStart()
112         if (mOnShowListener != null) {
113             mOnShowListener.onShow(getDialog());
114         }
115     }
116 
117     @Override
onCancel(DialogInterface dialog)118     public void onCancel(DialogInterface dialog) {
119         super.onCancel(dialog);
120         if (mOnCancelListener != null) {
121             mOnCancelListener.onCancel(dialog);
122         }
123     }
124 
125     @Override
onDismiss(DialogInterface dialog)126     public void onDismiss(DialogInterface dialog) {
127         super.onDismiss(dialog);
128         if (mOnDismissListener != null) {
129             mOnDismissListener.onDismiss(dialog);
130         }
131     }
132 
updateUserHandlesIfNeeded(Context context, Tile tile)133     public static void updateUserHandlesIfNeeded(Context context, Tile tile) {
134         final List<UserHandle> userHandles = tile.userHandle;
135         if (tile.userHandle == null || tile.userHandle.size() <= 1) {
136             return;
137         }
138         final UserManager userManager = UserManager.get(context);
139         for (int i = userHandles.size() - 1; i >= 0; i--) {
140             if (userManager.getUserInfo(userHandles.get(i).getIdentifier()) == null) {
141                 if (DEBUG) {
142                     Log.d(TAG, "Delete the user: " + userHandles.get(i).getIdentifier());
143                 }
144                 userHandles.remove(i);
145             }
146         }
147     }
148 }
149