]>
Commit | Line | Data |
---|---|---|
1 | package ro.ieval.fonbot; | |
2 | ||
3 | import java.io.IOException; | |
4 | ||
5 | import org.eclipse.jdt.annotation.Nullable; | |
6 | ||
7 | import android.app.admin.DevicePolicyManager; | |
8 | import android.content.ComponentName; | |
9 | import android.content.Intent; | |
10 | import android.content.pm.ApplicationInfo; | |
11 | import android.os.Bundle; | |
12 | import android.preference.CheckBoxPreference; | |
13 | import android.preference.EditTextPreference; | |
14 | import android.preference.Preference; | |
15 | import android.preference.Preference.OnPreferenceChangeListener; | |
16 | import android.preference.PreferenceActivity; | |
17 | import android.util.Log; | |
18 | import android.widget.Toast; | |
19 | ||
20 | /* | |
21 | * Copyright © 2013 Marius Gavrilescu | |
22 | * | |
23 | * This file is part of FonBot. | |
24 | * | |
25 | * FonBot is free software: you can redistribute it and/or modify | |
26 | * it under the terms of the GNU General Public License as published by | |
27 | * the Free Software Foundation, either version 3 of the License, or | |
28 | * (at your option) any later version. | |
29 | * | |
30 | * FonBot is distributed in the hope that it will be useful, | |
31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
33 | * GNU General Public License for more details. | |
34 | * | |
35 | * You should have received a copy of the GNU General Public License | |
36 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
37 | */ | |
38 | ||
39 | /** | |
40 | * Preference activity. Lets the user change app preferences. | |
41 | * | |
42 | * @author Marius Gavrilescu <marius@ieval.ro> | |
43 | */ | |
44 | public final class FonBotPreferenceActivity extends PreferenceActivity { | |
45 | /** | |
46 | * Preference for toggling device admin permissions. | |
47 | */ | |
48 | private CheckBoxPreference adminPreference; | |
49 | ||
50 | @SuppressWarnings("deprecation") | |
51 | @Override | |
52 | protected void onCreate(@Nullable final Bundle icicle) { | |
53 | super.onCreate(icicle); | |
54 | setTitle("FonBot Preferences"); | |
55 | addPreferencesFromResource(R.xml.prefs); | |
56 | ||
57 | adminPreference=(CheckBoxPreference) findPreference("admin"); | |
58 | final DevicePolicyManager dpm=(DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); | |
59 | final ComponentName adminReceiver=new ComponentName(this,FonBotAdminReceiver.class); | |
60 | adminPreference.setChecked(dpm.isAdminActive(adminReceiver)); | |
61 | ||
62 | adminPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
63 | ||
64 | @Override | |
65 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
66 | if(newValue==null){ | |
67 | Log.wtf(FonBotPreferenceActivity.class.getName(), "newValue in OnPreferenceChange is null"); | |
68 | throw new AssertionError("Log.wtf did not terminate the process"); | |
69 | } | |
70 | ||
71 | final boolean isChecked=((Boolean) newValue).booleanValue(); | |
72 | if(isChecked){ | |
73 | if(dpm.isAdminActive(adminReceiver)) | |
74 | return true; | |
75 | ||
76 | final Intent intent=new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); | |
77 | intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver); | |
78 | startActivityForResult(intent, 0); | |
79 | return false; | |
80 | } | |
81 | dpm.removeActiveAdmin(adminReceiver); | |
82 | return true; | |
83 | } | |
84 | }); | |
85 | ||
86 | final CheckBoxPreference foregroundPreference=(CheckBoxPreference) findPreference("foreground"); | |
87 | foregroundPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
88 | @Override | |
89 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
90 | startService(new Intent(FonBotPreferenceActivity.this, FonBotMainService.class)); | |
91 | return true; | |
92 | } | |
93 | }); | |
94 | ||
95 | final CheckBoxPreference systemPreference=(CheckBoxPreference) findPreference("system"); | |
96 | final ApplicationInfo info=getApplicationInfo(); | |
97 | final boolean isSystem=(info.flags&ApplicationInfo.FLAG_SYSTEM)!=0; | |
98 | systemPreference.setChecked(isSystem); | |
99 | systemPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
100 | @Override | |
101 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
102 | if(newValue==null) | |
103 | return false; | |
104 | final boolean isChecked=((Boolean)newValue).booleanValue(); | |
105 | if(isChecked==isSystem) | |
106 | return true; | |
107 | ||
108 | try { | |
109 | final String remountCommand="mount -o remount,rw /system"; | |
110 | final String copyToSystemCommand="cp "+info.sourceDir+" /system/app/FonBot.apk"; | |
111 | final String chmodSystemCommand="chmod 644 /system/app/FonBot.apk"; | |
112 | final String rmSystemCommand="rm /system/app/FonBot.apk"; | |
113 | final String copyToUserCommand="cp "+info.sourceDir+" /data/app/FonBot.apk"; | |
114 | final String chmodUserCommand="chmod 644 /data/app/FonBot.apk"; | |
115 | final String rmUserCommand="rm "+info.sourceDir; | |
116 | ||
117 | if(isChecked){ | |
118 | Runtime.getRuntime().exec(new String[]{ | |
119 | "su", "-c", | |
120 | remountCommand+';'+copyToSystemCommand+';'+chmodSystemCommand+';'+rmUserCommand | |
121 | }).waitFor(); | |
122 | ||
123 | Toast.makeText(FonBotPreferenceActivity.this, | |
124 | "Reboot to make FonBot a system application", Toast.LENGTH_LONG).show(); | |
125 | } else { | |
126 | Runtime.getRuntime().exec(new String[]{ | |
127 | "su", "-c", | |
128 | remountCommand+';'+copyToUserCommand+';'+chmodUserCommand+';'+rmSystemCommand | |
129 | }).waitFor(); | |
130 | Toast.makeText(FonBotPreferenceActivity.this, | |
131 | "Reboot to make FonBot a non-system application", Toast.LENGTH_LONG).show(); | |
132 | } | |
133 | } catch (IOException e) { | |
134 | return false; | |
135 | } catch (InterruptedException e) { | |
136 | return false; | |
137 | } | |
138 | return true; | |
139 | } | |
140 | }); | |
141 | ||
142 | final EditTextPreference protocolPreference=(EditTextPreference) findPreference("protocol"); | |
143 | protocolPreference.setSummary(protocolPreference.getText()); | |
144 | protocolPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
145 | @Override | |
146 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
147 | if(newValue==null) | |
148 | return false; | |
149 | ||
150 | protocolPreference.setSummary(newValue.toString()); | |
151 | return true; | |
152 | } | |
153 | }); | |
154 | ||
155 | final EditTextPreference hostnamePreference=(EditTextPreference) findPreference("hostname"); | |
156 | hostnamePreference.setSummary(hostnamePreference.getText()); | |
157 | hostnamePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
158 | @Override | |
159 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
160 | if(newValue==null) | |
161 | return false; | |
162 | ||
163 | hostnamePreference.setSummary(newValue.toString()); | |
164 | return true; | |
165 | } | |
166 | }); | |
167 | ||
168 | final EditTextPreference portPreference=(EditTextPreference) findPreference("port"); | |
169 | portPreference.setSummary(portPreference.getText()); | |
170 | portPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
171 | @Override | |
172 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
173 | if(newValue==null) | |
174 | return false; | |
175 | ||
176 | try{ | |
177 | Integer.parseInt(newValue.toString()); | |
178 | } catch (NumberFormatException e){ | |
179 | return false; | |
180 | } | |
181 | portPreference.setSummary(newValue.toString()); | |
182 | return true; | |
183 | } | |
184 | }); | |
185 | } | |
186 | ||
187 | @Override | |
188 | protected void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) { | |
189 | final DevicePolicyManager dpm=(DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); | |
190 | final ComponentName adminReceiver=new ComponentName(this,FonBotAdminReceiver.class); | |
191 | adminPreference.setChecked(dpm.isAdminActive(adminReceiver)); | |
192 | } | |
193 | ||
194 | } |