1 /* 2 * Copyright (C) 2021 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.deviceinfo.storage; 18 19 import android.content.Context; 20 import android.view.View; 21 import android.view.ViewGroup; 22 import android.widget.AdapterView; 23 import android.widget.TextView; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settingslib.widget.SettingsSpinnerPreference; 30 import com.android.settingslib.widget.settingsspinner.SettingsSpinnerAdapter; 31 32 import java.util.ArrayList; 33 import java.util.Collections; 34 import java.util.List; 35 36 /** 37 * Shows a spinner for users to select a storage volume. 38 */ 39 public class StorageSelectionPreferenceController extends BasePreferenceController implements 40 AdapterView.OnItemSelectedListener { 41 42 @VisibleForTesting 43 SettingsSpinnerPreference mSpinnerPreference; 44 @VisibleForTesting 45 StorageAdapter mStorageAdapter; 46 47 private final List<StorageEntry> mStorageEntries = new ArrayList<>(); 48 49 /** The interface for spinner selection callback. */ 50 public interface OnItemSelectedListener { 51 /** Callbacked when the spinner selection is changed. */ onItemSelected(StorageEntry storageEntry)52 void onItemSelected(StorageEntry storageEntry); 53 } 54 private OnItemSelectedListener mOnItemSelectedListener; 55 StorageSelectionPreferenceController(Context context, String key)56 public StorageSelectionPreferenceController(Context context, String key) { 57 super(context, key); 58 59 mStorageAdapter = new StorageAdapter(context); 60 } 61 setOnItemSelectedListener(OnItemSelectedListener listener)62 public void setOnItemSelectedListener(OnItemSelectedListener listener) { 63 mOnItemSelectedListener = listener; 64 } 65 66 /** Set the storages in the spinner. */ setStorageEntries(List<StorageEntry> storageEntries)67 public void setStorageEntries(List<StorageEntry> storageEntries) { 68 mStorageAdapter.clear(); 69 mStorageEntries.clear(); 70 if (storageEntries == null || storageEntries.isEmpty()) { 71 return; 72 } 73 Collections.sort(mStorageEntries); 74 mStorageEntries.addAll(storageEntries); 75 mStorageAdapter.addAll(storageEntries); 76 77 if (mSpinnerPreference != null) { 78 mSpinnerPreference.setVisible(mStorageAdapter.getCount() > 1); 79 } 80 } 81 82 /** set selected storage in the spinner. */ setSelectedStorageEntry(StorageEntry selectedStorageEntry)83 public void setSelectedStorageEntry(StorageEntry selectedStorageEntry) { 84 if (mSpinnerPreference == null || !mStorageEntries.contains(selectedStorageEntry)) { 85 return; 86 } 87 mSpinnerPreference.setSelection(mStorageAdapter.getPosition(selectedStorageEntry)); 88 } 89 90 @Override getAvailabilityStatus()91 public int getAvailabilityStatus() { 92 return AVAILABLE_UNSEARCHABLE; 93 } 94 95 @Override displayPreference(PreferenceScreen screen)96 public void displayPreference(PreferenceScreen screen) { 97 mSpinnerPreference = screen.findPreference(getPreferenceKey()); 98 mSpinnerPreference.setAdapter(mStorageAdapter); 99 mSpinnerPreference.setOnItemSelectedListener(this); 100 mSpinnerPreference.setVisible(mStorageAdapter.getCount() > 1); 101 } 102 103 @Override onItemSelected(AdapterView<?> arg0, View arg1, int position, long id)104 public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { 105 if (mOnItemSelectedListener == null) { 106 return; 107 } 108 mOnItemSelectedListener.onItemSelected( 109 (StorageEntry) mSpinnerPreference.getSelectedItem()); 110 } 111 112 @Override onNothingSelected(AdapterView<?> arg0)113 public void onNothingSelected(AdapterView<?> arg0) { 114 // Do nothing. 115 } 116 117 @VisibleForTesting 118 class StorageAdapter extends SettingsSpinnerAdapter<StorageEntry> { 119 StorageAdapter(Context context)120 StorageAdapter(Context context) { 121 super(context); 122 } 123 124 @Override getView(int position, View view, ViewGroup parent)125 public View getView(int position, View view, ViewGroup parent) { 126 if (view == null) { 127 view = getDefaultView(position, view, parent); 128 } 129 130 TextView textView = null; 131 try { 132 textView = (TextView) view; 133 } catch (ClassCastException e) { 134 throw new IllegalStateException("Default view should be a TextView, ", e); 135 } 136 textView.setText(getItem(position).getDescription()); 137 return textView; 138 } 139 140 @Override getDropDownView(int position, View view, ViewGroup parent)141 public View getDropDownView(int position, View view, ViewGroup parent) { 142 if (view == null) { 143 view = getDefaultDropDownView(position, view, parent); 144 } 145 146 TextView textView = null; 147 try { 148 textView = (TextView) view; 149 } catch (ClassCastException e) { 150 throw new IllegalStateException("Default drop down view should be a TextView, ", e); 151 } 152 textView.setText(getItem(position).getDescription()); 153 return textView; 154 } 155 } 156 } 157 158