11package tun .proxy ;
22
3+ import android .app .AlertDialog ;
34import android .annotation .TargetApi ;
45import android .content .Context ;
6+ import android .content .DialogInterface ;
57import android .content .Intent ;
68import android .content .pm .PackageInfo ;
79import android .content .pm .PackageManager ;
@@ -29,6 +31,7 @@ public class SimplePreferenceFragment extends PreferenceFragment
2931 public static final String VPN_CONNECTION_MODE = "vpn_connection_mode" ;
3032 public static final String VPN_DISALLOWED_APPLICATION_LIST = "vpn_disallowed_application_list" ;
3133 public static final String VPN_ALLOWED_APPLICATION_LIST = "vpn_allowed_application_list" ;
34+ public static final String VPN_CLEAR_ALL_SELECTION = "vpn_clear_all_selection" ;
3235
3336 @ Override
3437 public void onCreate (Bundle savedInstanceState ) {
@@ -40,8 +43,10 @@ public void onCreate(Bundle savedInstanceState) {
4043 final ListPreference prefPackage = (ListPreference ) this .findPreference (VPN_CONNECTION_MODE );
4144 final PreferenceScreen prefDisallow = (PreferenceScreen ) findPreference (VPN_DISALLOWED_APPLICATION_LIST );
4245 final PreferenceScreen prefAllow = (PreferenceScreen ) findPreference (VPN_ALLOWED_APPLICATION_LIST );
46+ final PreferenceScreen clearAllSelection = (PreferenceScreen ) findPreference (VPN_CLEAR_ALL_SELECTION );
4347 prefDisallow .setOnPreferenceClickListener (this );
4448 prefAllow .setOnPreferenceClickListener (this );
49+ clearAllSelection .setOnPreferenceClickListener (this );
4550
4651 prefPackage .setOnPreferenceChangeListener (new OnPreferenceChangeListener () {
4752 @ Override
@@ -63,6 +68,18 @@ public boolean onPreferenceChange(Preference preference, Object value) {
6368 prefDisallow .setEnabled (MyApplication .VPNMode .DISALLOW .name ().equals (prefPackage .getValue ()));
6469 prefAllow .setEnabled (MyApplication .VPNMode .ALLOW .name ().equals (prefPackage .getValue ()));
6570
71+ updateMenuItem ();
72+
73+ }
74+
75+ private void updateMenuItem () {
76+ final PreferenceScreen prefDisallow = (PreferenceScreen ) findPreference (VPN_DISALLOWED_APPLICATION_LIST );
77+ final PreferenceScreen prefAllow = (PreferenceScreen ) findPreference (VPN_ALLOWED_APPLICATION_LIST );
78+
79+ int countDisallow = MyApplication .getInstance ().loadVPNApplication (MyApplication .VPNMode .DISALLOW ).size ();
80+ int countAllow = MyApplication .getInstance ().loadVPNApplication (MyApplication .VPNMode .ALLOW ).size ();
81+ prefDisallow .setTitle (getString (R .string .pref_disallowed_application_list ) + String .format (" (%d)" ,countDisallow ));
82+ prefAllow .setTitle (getString (R .string .pref_allowed_application_list ) + String .format (" (%d)" ,countAllow ));
6683 }
6784
6885 @ Override
@@ -87,10 +104,29 @@ public boolean onPreferenceClick(Preference preference) {
87104 case VPN_ALLOWED_APPLICATION_LIST :
88105 transitionFragment (PackageListPreferenceFragment .newInstance (MyApplication .VPNMode .ALLOW ));
89106 break ;
107+ case VPN_CLEAR_ALL_SELECTION :
108+ new AlertDialog .Builder (getActivity ())
109+ .setTitle (getString (R .string .title_activity_settings ))
110+ .setMessage (getString (R .string .pref_dialog_clear_all_application_msg ))
111+ .setPositiveButton ("OK" , new DialogInterface .OnClickListener () {
112+ @ Override
113+ public void onClick (DialogInterface dialog , int which ) {
114+ Set set = new HashSet ();
115+ MyApplication .getInstance ().storeVPNApplication (MyApplication .VPNMode .ALLOW , set );
116+ MyApplication .getInstance ().storeVPNApplication (MyApplication .VPNMode .DISALLOW , set );
117+ updateMenuItem ();
118+ }
119+ })
120+ .setNegativeButton ("Cancel" , null )
121+ .show ();
122+
123+ break ;
90124 }
91125 return false ;
92126 }
93127
128+
129+
94130 private void transitionFragment (PreferenceFragment nextPreferenceFragment ) {
95131 // replaceによるFragmentの切り替えと、addToBackStackで戻るボタンを押した時に前のFragmentに戻るようにする
96132 getFragmentManager ()
0 commit comments