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 */
16package com.android.settings;
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;
25import android.os.Bundle;
26import android.os.UserHandle;
27import android.os.UserManager;
28
29import com.android.settings.dashboard.DashboardTile;
30
31public class ProfileSelectDialog extends DialogFragment implements OnClickListener {
32
33    private static final String ARG_SELECTED_TILE = "selectedTile";
34
35    private DashboardTile mSelectedTile;
36
37    public static void show(FragmentManager manager, DashboardTile tile) {
38        ProfileSelectDialog dialog = new ProfileSelectDialog();
39        Bundle args = new Bundle();
40        args.putParcelable(ARG_SELECTED_TILE, tile);
41        dialog.setArguments(args);
42        dialog.show(manager, "select_profile");
43    }
44
45    @Override
46    public void onCreate(Bundle savedInstanceState) {
47        super.onCreate(savedInstanceState);
48        mSelectedTile = getArguments().getParcelable(ARG_SELECTED_TILE);
49    }
50
51    @Override
52    public Dialog onCreateDialog(Bundle savedInstanceState) {
53        Context context = getActivity();
54        AlertDialog.Builder builder = new AlertDialog.Builder(context);
55        UserAdapter adapter = Utils.createUserAdapter(UserManager.get(context), context,
56                mSelectedTile.userHandle);
57        builder.setTitle(R.string.choose_profile)
58                .setAdapter(adapter, this);
59
60        return builder.create();
61    }
62
63    @Override
64    public void onClick(DialogInterface dialog, int which) {
65        UserHandle user = mSelectedTile.userHandle.get(which);
66        getActivity().startActivityAsUser(mSelectedTile.intent, user);
67    }
68}
69