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