OSDN Git Service

One updateSummary method called by all VPN prefs
authorRobin Lee <rgl@google.com>
Tue, 19 Apr 2016 11:29:02 +0000 (12:29 +0100)
committerRobin Lee <rgl@google.com>
Tue, 19 Apr 2016 19:09:19 +0000 (19:09 +0000)
Having multiple methods means inevitably when new features are added to
the preferences, the right calls aren't made so information on the
screen lags between updates.

Bug: 28257641
Change-Id: I336aeefd5941ccf808dc9070427209a7d2530032

src/com/android/settings/vpn2/AppPreference.java
src/com/android/settings/vpn2/LegacyVpnPreference.java
src/com/android/settings/vpn2/ManageablePreference.java

index e3649a8..fa9daf6 100644 (file)
@@ -32,9 +32,8 @@ import com.android.internal.net.VpnConfig;
  */
 public class AppPreference extends ManageablePreference {
     public static final int STATE_CONNECTED = LegacyVpnInfo.STATE_CONNECTED;
-    public static final int STATE_DISCONNECTED = LegacyVpnInfo.STATE_DISCONNECTED;
+    public static final int STATE_DISCONNECTED = STATE_NONE;
 
-    private int mState = STATE_DISCONNECTED;
     private String mPackageName;
     private String mName;
 
@@ -70,22 +69,11 @@ public class AppPreference extends ManageablePreference {
         update();
     }
 
-    public int getState() {
-        return mState;
-    }
-
-    public void setState(int state) {
-        mState = state;
-        update();
-    }
-
     private void update() {
         if (mPackageName == null || mUserId == UserHandle.USER_NULL) {
             return;
         }
 
-        setSummary(getSummaryString(mState == STATE_DISCONNECTED ? STATE_NONE : mState));
-
         mName = mPackageName;
         Drawable icon = null;
 
@@ -113,6 +101,7 @@ public class AppPreference extends ManageablePreference {
         }
         setTitle(mName);
         setIcon(icon);
+        updateSummary();
 
         notifyHierarchyChanged();
     }
index a9b8aec..2ce22d4 100644 (file)
@@ -31,9 +31,6 @@ import static com.android.internal.net.LegacyVpnInfo.STATE_CONNECTED;
 public class LegacyVpnPreference extends ManageablePreference {
     private VpnProfile mProfile;
 
-    /** One of the STATE_* fields from LegacyVpnInfo, or STATE_NONE */
-    private int mState = STATE_NONE;
-
     LegacyVpnPreference(Context context) {
         super(context, null /* attrs */);
     }
@@ -44,16 +41,6 @@ public class LegacyVpnPreference extends ManageablePreference {
 
     public void setProfile(VpnProfile profile) {
         mProfile = profile;
-        update();
-    }
-
-    public void setState(int state) {
-        mState = state;
-        update();
-    }
-
-    private void update() {
-        setSummary(getSummaryString(mState));
         if (mProfile != null) {
             setIcon(R.mipmap.ic_launcher_settings);
             setTitle(mProfile.name);
@@ -93,5 +80,4 @@ public class LegacyVpnPreference extends ManageablePreference {
         }
         super.onClick(v);
     }
-
-}
\ No newline at end of file
+}
index ad8a8a3..7c07e20 100644 (file)
@@ -34,6 +34,7 @@ public abstract class ManageablePreference extends GearPreference {
     public static int STATE_NONE = -1;
 
     boolean mIsAlwaysOn = false;
+    int mState = STATE_NONE;
     int mUserId;
 
     public ManageablePreference(Context context, AttributeSet attrs) {
@@ -56,24 +57,35 @@ public abstract class ManageablePreference extends GearPreference {
         return mIsAlwaysOn;
     }
 
+    public int getState() {
+        return mState;
+    }
+
+    public void setState(int state) {
+        mState = state;
+        updateSummary();
+    }
+
     public void setAlwaysOn(boolean isEnabled) {
         mIsAlwaysOn = isEnabled;
+        updateSummary();
     }
 
     /**
-     * State is not shown for {@code STATE_NONE}
+     * Update the preference summary string (see {@see Preference#setSummary}) with a string
+     * reflecting connection status and always-on setting.
      *
-     * @return summary string showing current connection state and always-on-vpn state
+     * State is not shown for {@code STATE_NONE}.
      */
-    protected String getSummaryString(int state) {
+    protected void updateSummary() {
         final Resources res = getContext().getResources();
         final String[] states = res.getStringArray(R.array.vpn_states);
-        String summary = state == STATE_NONE ? "" : states[state];
+        String summary = (mState == STATE_NONE ? "" : states[mState]);
         if (mIsAlwaysOn) {
             final String alwaysOnString = res.getString(R.string.vpn_always_on_active);
             summary = TextUtils.isEmpty(summary) ? alwaysOnString : res.getString(
                     R.string.join_two_unrelated_items, summary, alwaysOnString);
         }
-        return summary;
+        setSummary(summary);
     }
 }