| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.preference; |
| 18 |
|
| 19 |
import android.app.Dialog; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.DialogInterface; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.graphics.drawable.Drawable; |
| 25 |
import android.os.Build; |
| 26 |
import android.os.Bundle; |
| 27 |
import android.os.Parcel; |
| 28 |
import android.os.Parcelable; |
| 29 |
import android.text.TextUtils; |
| 30 |
import android.util.AttributeSet; |
| 31 |
import android.view.LayoutInflater; |
| 32 |
import android.view.View; |
| 33 |
import android.view.Window; |
| 34 |
import android.widget.Adapter; |
| 35 |
import android.widget.AdapterView; |
| 36 |
import android.widget.ListAdapter; |
| 37 |
import android.widget.ListView; |
| 38 |
import android.widget.TextView; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
@Deprecated |
| 98 |
public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener, |
| 99 |
DialogInterface.OnDismissListener { |
| 100 |
|
| 101 |
@UnsupportedAppUsage |
| 102 |
private ListAdapter mRootAdapter; |
| 103 |
|
| 104 |
private Dialog mDialog; |
| 105 |
|
| 106 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 107 |
private ListView mListView; |
| 108 |
|
| 109 |
private int mLayoutResId = com.android.internal.R.layout.preference_list_fragment; |
| 110 |
private Drawable mDividerDrawable; |
| 111 |
private boolean mDividerSpecified; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@UnsupportedAppUsage |
| 118 |
public PreferenceScreen(Context context, AttributeSet attrs) { |
| 119 |
super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle); |
| 120 |
|
| 121 |
TypedArray a = context.obtainStyledAttributes(null, |
| 122 |
com.android.internal.R.styleable.PreferenceScreen, |
| 123 |
com.android.internal.R.attr.preferenceScreenStyle, |
| 124 |
0); |
| 125 |
|
| 126 |
mLayoutResId = a.getResourceId( |
| 127 |
com.android.internal.R.styleable.PreferenceScreen_screenLayout, |
| 128 |
mLayoutResId); |
| 129 |
if (a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceScreen_divider)) { |
| 130 |
mDividerDrawable = |
| 131 |
a.getDrawable(com.android.internal.R.styleable.PreferenceScreen_divider); |
| 132 |
mDividerSpecified = true; |
| 133 |
} |
| 134 |
|
| 135 |
a.recycle(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public ListAdapter getRootAdapter() { |
| 153 |
if (mRootAdapter == null) { |
| 154 |
mRootAdapter = onCreateRootAdapter(); |
| 155 |
} |
| 156 |
|
| 157 |
return mRootAdapter; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
protected ListAdapter onCreateRootAdapter() { |
| 167 |
return new PreferenceGroupAdapter(this); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public void bind(ListView listView) { |
| 178 |
listView.setOnItemClickListener(this); |
| 179 |
listView.setAdapter(getRootAdapter()); |
| 180 |
|
| 181 |
onAttachedToActivity(); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
protected void onClick() { |
| 186 |
if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
showDialog(null); |
| 191 |
} |
| 192 |
|
| 193 |
private void showDialog(Bundle state) { |
| 194 |
Context context = getContext(); |
| 195 |
if (mListView != null) { |
| 196 |
mListView.setAdapter(null); |
| 197 |
} |
| 198 |
|
| 199 |
LayoutInflater inflater = (LayoutInflater) |
| 200 |
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 201 |
View childPrefScreen = inflater.inflate(mLayoutResId, null); |
| 202 |
View titleView = childPrefScreen.findViewById(android.R.id.title); |
| 203 |
mListView = (ListView) childPrefScreen.findViewById(android.R.id.list); |
| 204 |
if (mDividerSpecified) { |
| 205 |
mListView.setDivider(mDividerDrawable); |
| 206 |
} |
| 207 |
|
| 208 |
bind(mListView); |
| 209 |
|
| 210 |
|
| 211 |
final CharSequence title = getTitle(); |
| 212 |
Dialog dialog = mDialog = new Dialog(context, context.getThemeResId()); |
| 213 |
if (TextUtils.isEmpty(title)) { |
| 214 |
if (titleView != null) { |
| 215 |
titleView.setVisibility(View.GONE); |
| 216 |
} |
| 217 |
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); |
| 218 |
} else { |
| 219 |
if (titleView instanceof TextView) { |
| 220 |
((TextView) titleView).setText(title); |
| 221 |
titleView.setVisibility(View.VISIBLE); |
| 222 |
} else { |
| 223 |
dialog.setTitle(title); |
| 224 |
} |
| 225 |
} |
| 226 |
dialog.setContentView(childPrefScreen); |
| 227 |
dialog.setOnDismissListener(this); |
| 228 |
if (state != null) { |
| 229 |
dialog.onRestoreInstanceState(state); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
getPreferenceManager().addPreferencesScreen(dialog); |
| 234 |
|
| 235 |
dialog.show(); |
| 236 |
} |
| 237 |
|
| 238 |
public void onDismiss(DialogInterface dialog) { |
| 239 |
mDialog = null; |
| 240 |
getPreferenceManager().removePreferencesScreen(dialog); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public Dialog getDialog() { |
| 249 |
return mDialog; |
| 250 |
} |
| 251 |
|
| 252 |
public void onItemClick(AdapterView parent, View view, int position, long id) { |
| 253 |
|
| 254 |
if (parent instanceof ListView) { |
| 255 |
position -= ((ListView) parent).getHeaderViewsCount(); |
| 256 |
} |
| 257 |
Object item = getRootAdapter().getItem(position); |
| 258 |
if (!(item instanceof Preference)) return; |
| 259 |
|
| 260 |
final Preference preference = (Preference) item; |
| 261 |
preference.performClick(this); |
| 262 |
} |
| 263 |
|
| 264 |
@Override |
| 265 |
protected boolean isOnSameScreenAsChildren() { |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
@Override |
| 270 |
protected Parcelable onSaveInstanceState() { |
| 271 |
final Parcelable superState = super.onSaveInstanceState(); |
| 272 |
final Dialog dialog = mDialog; |
| 273 |
if (dialog == null || !dialog.isShowing()) { |
| 274 |
return superState; |
| 275 |
} |
| 276 |
|
| 277 |
final SavedState myState = new SavedState(superState); |
| 278 |
myState.isDialogShowing = true; |
| 279 |
myState.dialogBundle = dialog.onSaveInstanceState(); |
| 280 |
return myState; |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
protected void onRestoreInstanceState(Parcelable state) { |
| 285 |
if (state == null || !state.getClass().equals(SavedState.class)) { |
| 286 |
|
| 287 |
super.onRestoreInstanceState(state); |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
SavedState myState = (SavedState) state; |
| 292 |
super.onRestoreInstanceState(myState.getSuperState()); |
| 293 |
if (myState.isDialogShowing) { |
| 294 |
showDialog(myState.dialogBundle); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
private static class SavedState extends BaseSavedState { |
| 299 |
boolean isDialogShowing; |
| 300 |
Bundle dialogBundle; |
| 301 |
|
| 302 |
public SavedState(Parcel source) { |
| 303 |
super(source); |
| 304 |
isDialogShowing = source.readInt() == 1; |
| 305 |
dialogBundle = source.readBundle(); |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public void writeToParcel(Parcel dest, int flags) { |
| 310 |
super.writeToParcel(dest, flags); |
| 311 |
dest.writeInt(isDialogShowing ? 1 : 0); |
| 312 |
dest.writeBundle(dialogBundle); |
| 313 |
} |
| 314 |
|
| 315 |
public SavedState(Parcelable superState) { |
| 316 |
super(superState); |
| 317 |
} |
| 318 |
|
| 319 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 320 |
new Parcelable.Creator<SavedState>() { |
| 321 |
public SavedState createFromParcel(Parcel in) { |
| 322 |
return new SavedState(in); |
| 323 |
} |
| 324 |
|
| 325 |
public SavedState[] newArray(int size) { |
| 326 |
return new SavedState[size]; |
| 327 |
} |
| 328 |
}; |
| 329 |
} |
| 330 |
|
| 331 |
} |