1 package ro
.ieval
.fonbot
;
3 import java
.io
.IOException
;
5 import org
.eclipse
.jdt
.annotation
.Nullable
;
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
;
21 * Copyright © 2013 Marius Gavrilescu
23 * This file is part of FonBot.
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.
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.
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/>.
40 * Preference activity. Lets the user change app preferences.
42 * @author Marius Gavrilescu <marius@ieval.ro>
44 public final class FonBotPreferenceActivity
extends PreferenceActivity
{
46 * Preference for toggling device admin permissions.
48 private CheckBoxPreference adminPreference
;
50 @SuppressWarnings("deprecation")
52 protected void onCreate(@Nullable final Bundle icicle
) {
53 super.onCreate(icicle
);
54 setTitle("FonBot Preferences");
55 addPreferencesFromResource(R
.xml
.prefs
);
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
));
62 adminPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
65 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
67 Log
.wtf(FonBotPreferenceActivity
.class.getName(), "newValue in OnPreferenceChange is null");
68 throw new AssertionError("Log.wtf did not terminate the process");
71 final boolean isChecked
=((Boolean
) newValue
).booleanValue();
73 if(dpm
.isAdminActive(adminReceiver
))
76 final Intent intent
=new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN
);
77 intent
.putExtra(DevicePolicyManager
.EXTRA_DEVICE_ADMIN
, adminReceiver
);
78 startActivityForResult(intent
, 0);
81 dpm
.removeActiveAdmin(adminReceiver
);
86 final CheckBoxPreference foregroundPreference
=(CheckBoxPreference
) findPreference("foreground");
87 foregroundPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
89 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
90 startService(new Intent(FonBotPreferenceActivity
.this, FonBotMainService
.class));
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() {
101 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
104 final boolean isChecked
=((Boolean
)newValue
).booleanValue();
105 if(isChecked
==isSystem
)
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
;
118 Runtime
.getRuntime().exec(new String
[]{
120 remountCommand
+';'+copyToSystemCommand
+';'+chmodSystemCommand
+';'+rmUserCommand
123 Toast
.makeText(FonBotPreferenceActivity
.this,
124 "Reboot to make FonBot a system application", Toast
.LENGTH_LONG
).show();
126 Runtime
.getRuntime().exec(new String
[]{
128 remountCommand
+';'+copyToUserCommand
+';'+chmodUserCommand
+';'+rmSystemCommand
130 Toast
.makeText(FonBotPreferenceActivity
.this,
131 "Reboot to make FonBot a non-system application", Toast
.LENGTH_LONG
).show();
133 } catch (IOException e
) {
135 } catch (InterruptedException e
) {
142 final EditTextPreference protocolPreference
=(EditTextPreference
) findPreference("protocol");
143 protocolPreference
.setSummary(protocolPreference
.getText());
144 protocolPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
146 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
150 protocolPreference
.setSummary(newValue
.toString());
155 final EditTextPreference hostnamePreference
=(EditTextPreference
) findPreference("hostname");
156 hostnamePreference
.setSummary(hostnamePreference
.getText());
157 hostnamePreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
159 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
163 hostnamePreference
.setSummary(newValue
.toString());
168 final EditTextPreference portPreference
=(EditTextPreference
) findPreference("port");
169 portPreference
.setSummary(portPreference
.getText());
170 portPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
172 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
177 Integer
.parseInt(newValue
.toString());
178 } catch (NumberFormatException e
){
181 portPreference
.setSummary(newValue
.toString());
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
));