1 /*
2  * Copyright (C) 2016 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.print;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.view.View;
26 import android.widget.AdapterView;
27 import android.widget.Spinner;
28 
29 import com.android.settings.R;
30 import com.android.settings.dashboard.RestrictedDashboardFragment;
31 import com.android.settings.dashboard.profileselector.UserAdapter;
32 
33 /**
34  * Base fragment class for per profile settings.
35  */
36 public abstract class ProfileSettingsPreferenceFragment extends RestrictedDashboardFragment {
37 
ProfileSettingsPreferenceFragment(String restrictionKey)38     public ProfileSettingsPreferenceFragment(String restrictionKey) {
39         super(restrictionKey);
40     }
41 
42     @Override
onViewCreated(View view, Bundle savedInstanceState)43     public void onViewCreated(View view, Bundle savedInstanceState) {
44         super.onViewCreated(view, savedInstanceState);
45         final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
46         final UserAdapter profileSpinnerAdapter =
47                 UserAdapter.createUserSpinnerAdapter(um, getActivity());
48         if (profileSpinnerAdapter != null) {
49             final Spinner spinner = (Spinner) setPinnedHeaderView(R.layout.spinner_view);
50             spinner.setAdapter(profileSpinnerAdapter);
51             spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
52                 @Override
53                 public void onItemSelected(AdapterView<?> parent, View view, int position,
54                         long id) {
55                     final UserHandle selectedUser = profileSpinnerAdapter.getUserHandle(position);
56                     if (selectedUser.getIdentifier() != UserHandle.myUserId()) {
57                         final Activity activity = getActivity();
58                         Intent intent = new Intent(getIntentActionString())
59                                 .setPackage(getContext().getPackageName());
60                         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
61                         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
62                         activity.startActivityAsUser(intent, selectedUser);
63                         // Go back to default selection, which is the first one
64                         spinner.setSelection(0);
65                         activity.finish();
66                     }
67                 }
68 
69                 @Override
70                 public void onNothingSelected(AdapterView<?> parent) {
71                     // Nothing to do
72                 }
73             });
74         }
75     }
76 
77     /**
78      * @return intent action string that will bring user to this fragment.
79      */
getIntentActionString()80     protected abstract String getIntentActionString();
81 
82 }
83