1 /*
2  * Copyright (C) 2012 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;
18 
19 import android.content.Context;
20 import android.preference.EditTextPreference;
21 import android.preference.ListPreference;
22 import android.text.TextUtils;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.EditText;
26 
27 public class SelectableEditTextPreference extends EditTextPreference {
28 
29     private int mSelectionMode;
30 
31     public static final int SELECTION_CURSOR_END   = 0;
32     public static final int SELECTION_CURSOR_START = 1;
33     public static final int SELECTION_SELECT_ALL   = 2;
34 
SelectableEditTextPreference(Context context, AttributeSet attrs)35     public SelectableEditTextPreference(Context context, AttributeSet attrs) {
36         super(context, attrs);
37     }
38 
39     /**
40      * Sets the selection mode for the text when it shows up in the dialog
41      * @hide
42      * @param selectionMode can be SELECTION_CURSOR_START, SELECTION_CURSOR_END or
43      * SELECTION_SELECT_ALL. Default is SELECTION_CURSOR_END
44      */
setInitialSelectionMode(int selectionMode)45     public void setInitialSelectionMode(int selectionMode) {
46         mSelectionMode = selectionMode;
47     }
48 
49     @Override
onBindDialogView(View view)50     protected void onBindDialogView(View view) {
51         super.onBindDialogView(view);
52 
53         EditText editText = getEditText();
54         // Set the selection based on the mSelectionMode
55         int length = editText.getText() != null ? editText.getText().length() : 0;
56         if (!TextUtils.isEmpty(editText.getText())) {
57             switch (mSelectionMode) {
58             case SELECTION_CURSOR_END:
59                 editText.setSelection(length);
60                 break;
61             case SELECTION_CURSOR_START:
62                 editText.setSelection(0);
63                 break;
64             case SELECTION_SELECT_ALL:
65                 editText.setSelection(0, length);
66                 break;
67             }
68         }
69     }
70 }
71 
72