blob: 512049fc1eaf0790453ca7d63131c5e69b3caea0 [file] [log] [blame]
Jason Monk0d72d202015-11-04 13:16:00 -05001/*
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 */
16package com.android.settingslib.drawer;
17
18import android.app.AlertDialog;
19import android.app.Dialog;
20import android.app.DialogFragment;
21import android.app.FragmentManager;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnClickListener;
Jason Monkcafda1f2016-02-05 10:16:22 -050025import android.content.Intent;
Jason Monk0d72d202015-11-04 13:16:00 -050026import android.os.Bundle;
27import android.os.UserHandle;
28import android.os.UserManager;
Fan Zhang11b65072016-10-31 16:45:00 -070029import android.util.Log;
30
31import java.util.List;
Jason Monk0d72d202015-11-04 13:16:00 -050032
33public class ProfileSelectDialog extends DialogFragment implements OnClickListener {
34
Fan Zhang11b65072016-10-31 16:45:00 -070035 private static final String TAG = "ProfileSelectDialog";
Jason Monk0d72d202015-11-04 13:16:00 -050036 private static final String ARG_SELECTED_TILE = "selectedTile";
Fan Zhang11b65072016-10-31 16:45:00 -070037 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jason Monk0d72d202015-11-04 13:16:00 -050038
Jason Monkf509d7e2016-01-07 16:22:53 -050039 private Tile mSelectedTile;
Jason Monk0d72d202015-11-04 13:16:00 -050040
Jason Monkf509d7e2016-01-07 16:22:53 -050041 public static void show(FragmentManager manager, Tile tile) {
Jason Monk0d72d202015-11-04 13:16:00 -050042 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
50 public void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 mSelectedTile = getArguments().getParcelable(ARG_SELECTED_TILE);
53 }
54
55 @Override
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
68 public void onClick(DialogInterface dialog, int which) {
69 UserHandle user = mSelectedTile.userHandle.get(which);
Jason Monk946483a2016-01-22 15:19:44 -050070 // Show menu on top level items.
71 mSelectedTile.intent.putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true);
Jason Monkcafda1f2016-02-05 10:16:22 -050072 mSelectedTile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Jason Monk0d72d202015-11-04 13:16:00 -050073 getActivity().startActivityAsUser(mSelectedTile.intent, user);
74 ((SettingsDrawerActivity) getActivity()).onProfileTileOpen();
75 }
Fan Zhang11b65072016-10-31 16:45:00 -070076
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 }
Jason Monk0d72d202015-11-04 13:16:00 -050092}