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.SettingsPreferenceFragment; 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 SettingsPreferenceFragment { 37 38 @Override onViewCreated(View view, Bundle savedInstanceState)39 public void onViewCreated(View view, Bundle savedInstanceState) { 40 super.onViewCreated(view, savedInstanceState); 41 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE); 42 final UserAdapter profileSpinnerAdapter = 43 UserAdapter.createUserSpinnerAdapter(um, getActivity()); 44 if (profileSpinnerAdapter != null) { 45 final Spinner spinner = (Spinner) setPinnedHeaderView(R.layout.spinner_view); 46 spinner.setAdapter(profileSpinnerAdapter); 47 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 48 @Override 49 public void onItemSelected(AdapterView<?> parent, View view, int position, 50 long id) { 51 final UserHandle selectedUser = profileSpinnerAdapter.getUserHandle(position); 52 if (selectedUser.getIdentifier() != UserHandle.myUserId()) { 53 final Activity activity = getActivity(); 54 Intent intent = new Intent(getIntentActionString()); 55 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 56 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 57 activity.startActivityAsUser(intent, selectedUser); 58 // Go back to default selection, which is the first one 59 spinner.setSelection(0); 60 activity.finish(); 61 } 62 } 63 64 @Override 65 public void onNothingSelected(AdapterView<?> parent) { 66 // Nothing to do 67 } 68 }); 69 } 70 } 71 72 /** 73 * @return intent action string that will bring user to this fragment. 74 */ getIntentActionString()75 protected abstract String getIntentActionString(); 76 77 } 78