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