Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 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; |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 25 | import android.content.Intent; |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 26 | import android.os.Bundle; |
| 27 | import android.os.UserHandle; |
| 28 | import android.os.UserManager; |
Fan Zhang | 11b6507 | 2016-10-31 16:45:00 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
| 31 | import java.util.List; |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 32 | |
| 33 | public class ProfileSelectDialog extends DialogFragment implements OnClickListener { |
| 34 | |
Fan Zhang | 11b6507 | 2016-10-31 16:45:00 -0700 | [diff] [blame] | 35 | private static final String TAG = "ProfileSelectDialog"; |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 36 | private static final String ARG_SELECTED_TILE = "selectedTile"; |
Fan Zhang | 11b6507 | 2016-10-31 16:45:00 -0700 | [diff] [blame] | 37 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 38 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 39 | private Tile mSelectedTile; |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 40 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 41 | public static void show(FragmentManager manager, Tile tile) { |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 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 |
| 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 Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 70 | // Show menu on top level items. |
| 71 | mSelectedTile.intent.putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true); |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 72 | mSelectedTile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 73 | getActivity().startActivityAsUser(mSelectedTile.intent, user); |
| 74 | ((SettingsDrawerActivity) getActivity()).onProfileTileOpen(); |
| 75 | } |
Fan Zhang | 11b6507 | 2016-10-31 16:45:00 -0700 | [diff] [blame] | 76 | |
| 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 Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 92 | } |