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.settings.overlay.FeatureFactory; 34 import com.android.settingslib.drawer.Tile; 35 36 import java.util.List; 37 38 public class ProfileSelectDialog extends DialogFragment implements OnClickListener { 39 40 private static final String TAG = "ProfileSelectDialog"; 41 private static final String ARG_SELECTED_TILE = "selectedTile"; 42 private static final String ARG_SOURCE_METRIC_CATEGORY = "sourceMetricCategory"; 43 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 44 45 private int mSourceMetricCategory; 46 private Tile mSelectedTile; 47 48 /** 49 * Display the profile select dialog, adding the fragment to the given FragmentManager. 50 * @param manager The FragmentManager this fragment will be added to. 51 * @param tile The tile for this fragment. 52 * @param sourceMetricCategory The source metric category. 53 */ show(FragmentManager manager, Tile tile, int sourceMetricCategory)54 public static void show(FragmentManager manager, Tile tile, int sourceMetricCategory) { 55 final ProfileSelectDialog dialog = new ProfileSelectDialog(); 56 final Bundle args = new Bundle(); 57 args.putParcelable(ARG_SELECTED_TILE, tile); 58 args.putInt(ARG_SOURCE_METRIC_CATEGORY, sourceMetricCategory); 59 dialog.setArguments(args); 60 dialog.show(manager, "select_profile"); 61 } 62 63 @Override onCreate(Bundle savedInstanceState)64 public void onCreate(Bundle savedInstanceState) { 65 super.onCreate(savedInstanceState); 66 mSelectedTile = getArguments().getParcelable(ARG_SELECTED_TILE); 67 mSourceMetricCategory = getArguments().getInt(ARG_SOURCE_METRIC_CATEGORY); 68 } 69 70 @Override onCreateDialog(Bundle savedInstanceState)71 public Dialog onCreateDialog(Bundle savedInstanceState) { 72 final Context context = getActivity(); 73 final AlertDialog.Builder builder = new AlertDialog.Builder(context); 74 final UserAdapter adapter = UserAdapter.createUserAdapter(UserManager.get(context), context, 75 mSelectedTile.userHandle); 76 builder.setTitle(com.android.settingslib.R.string.choose_profile) 77 .setAdapter(adapter, this); 78 79 return builder.create(); 80 } 81 82 @Override onClick(DialogInterface dialog, int which)83 public void onClick(DialogInterface dialog, int which) { 84 final UserHandle user = mSelectedTile.userHandle.get(which); 85 // Show menu on top level items. 86 final Intent intent = mSelectedTile.getIntent(); 87 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider() 88 .logStartedIntentWithProfile(intent, mSourceMetricCategory, 89 which == 1 /* isWorkProfile */); 90 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 91 getActivity().startActivityAsUser(intent, user); 92 } 93 updateUserHandlesIfNeeded(Context context, Tile tile)94 public static void updateUserHandlesIfNeeded(Context context, Tile tile) { 95 final List<UserHandle> userHandles = tile.userHandle; 96 if (tile.userHandle == null || tile.userHandle.size() <= 1) { 97 return; 98 } 99 final UserManager userManager = UserManager.get(context); 100 for (int i = userHandles.size() - 1; i >= 0; i--) { 101 if (userManager.getUserInfo(userHandles.get(i).getIdentifier()) == null) { 102 if (DEBUG) { 103 Log.d(TAG, "Delete the user: " + userHandles.get(i).getIdentifier()); 104 } 105 userHandles.remove(i); 106 } 107 } 108 } 109 } 110