OSDN Git Service

Merge tag 'android-6.0.1_r74' into HEAD
[android-x86/packages-apps-Settings.git] / src / com / android / settings / HostnamePreference.java
1 /*
2  * Copyright (C) 2013 The CyanogenMod 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.os.SystemProperties;
21 import android.preference.EditTextPreference;
22 import android.provider.Settings;
23 import android.text.InputFilter;
24 import android.text.Spanned;
25 import android.util.AttributeSet;
26 import android.util.Log;
27
28 import cyanogenmod.providers.CMSettings;
29
30 public class HostnamePreference extends EditTextPreference {
31
32     private static final String TAG = "HostnamePreference";
33
34     private static final String PROP_HOSTNAME = "net.hostname";
35
36     private final String DEFAULT_HOSTNAME;
37
38     InputFilter mHostnameInputFilter = new InputFilter() {
39         @Override
40         public CharSequence filter(CharSequence source, int start, int end,
41                 Spanned dest, int dstart, int dend) {
42
43             if (source.length() == 0)
44                 return null;
45
46             // remove any character that is not alphanumeric, period, or hyphen
47             return source.subSequence(start, end).toString().replaceAll("[^-.a-zA-Z0-9]", "");
48         }
49     };
50
51     public HostnamePreference(Context context, AttributeSet attrs, int defStyle) {
52         super(context, attrs, defStyle);
53
54         // determine the default hostname
55         String id = Settings.Secure.getString(getContext().getContentResolver(),
56                 Settings.Secure.ANDROID_ID);
57         if (id != null && id.length() > 0) {
58             DEFAULT_HOSTNAME =  "android-".concat(id);
59         } else {
60             DEFAULT_HOSTNAME = "";
61         }
62
63         setSummary(getText());
64         getEditText().setFilters(new InputFilter[] { mHostnameInputFilter });
65         getEditText().setHint(DEFAULT_HOSTNAME);
66     }
67
68     public HostnamePreference(Context context, AttributeSet attrs) {
69         this(context, attrs, com.android.internal.R.attr.editTextPreferenceStyle);
70     }
71
72     public HostnamePreference(Context context) {
73         this(context, null);
74     }
75
76     @Override
77     protected void onDialogClosed(boolean positiveResult) {
78         if (positiveResult) {
79             String hostname = getEditText().getText().toString();
80
81             // remove any preceding or succeeding periods or hyphens
82             hostname = hostname.replaceAll("(?:\\.|-)+$", "");
83             hostname = hostname.replaceAll("^(?:\\.|-)+", "");
84
85             if (hostname.length() == 0) {
86                 if (DEFAULT_HOSTNAME.length() != 0) {
87                     // if no hostname is given, use the default
88                     hostname = DEFAULT_HOSTNAME;
89                 } else {
90                     // if no other name can be determined
91                     // fall back on the current hostname
92                     hostname = getText();
93                 }
94             }
95             setText(hostname);
96         }
97     }
98
99     @Override
100     public void setText(String text) {
101         if (text == null) {
102             Log.e(TAG, "tried to set null hostname, request ignored");
103             return;
104         } else if (text.length() == 0) {
105             Log.w(TAG, "setting empty hostname");
106         } else {
107             Log.i(TAG, "hostname has been set: " + text);
108         }
109         SystemProperties.set(PROP_HOSTNAME, text);
110         persistHostname(text);
111         setSummary(text);
112     }
113
114     @Override
115     public String getText() {
116         return SystemProperties.get(PROP_HOSTNAME);
117     }
118
119     @Override
120     public void onSetInitialValue(boolean restoreValue, Object defaultValue) {
121         String hostname = getText();
122         persistHostname(hostname);
123     }
124
125     public void persistHostname(String hostname) {
126         CMSettings.Secure.putString(getContext().getContentResolver(),
127                 CMSettings.Secure.DEVICE_HOSTNAME, hostname);
128     }
129 }