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
.Preference
;
14 import android
.preference
.Preference
.OnPreferenceChangeListener
;
15 import android
.preference
.PreferenceActivity
;
16 import android
.util
.Log
;
17 import android
.widget
.Toast
;
20 * Copyright © 2013 Marius Gavrilescu
22 * This file is part of FonBot.
24 * FonBot is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * FonBot is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
39 * Preference activity. Lets the user change app preferences.
41 * @author Marius Gavrilescu <marius@ieval.ro>
43 public final class FonBotPreferenceActivity
extends PreferenceActivity
{
45 * Preference for toggling device admin permissions.
47 private CheckBoxPreference adminPreference
;
49 @SuppressWarnings("deprecation")
51 protected void onCreate(@Nullable final Bundle icicle
) {
52 super.onCreate(icicle
);
53 setTitle("FonBot Preferences");
54 addPreferencesFromResource(R
.xml
.prefs
);
56 adminPreference
=(CheckBoxPreference
) findPreference("admin");
57 final DevicePolicyManager dpm
=(DevicePolicyManager
) getSystemService(DEVICE_POLICY_SERVICE
);
58 final ComponentName adminReceiver
=new ComponentName(this,FonBotAdminReceiver
.class);
59 adminPreference
.setChecked(dpm
.isAdminActive(adminReceiver
));
61 adminPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
64 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
66 Log
.wtf(FonBotPreferenceActivity
.class.getName(), "newValue in OnPreferenceChange is null");
67 throw new AssertionError("Log.wtf did not terminate the process");
70 final boolean isChecked
=((Boolean
) newValue
).booleanValue();
72 if(dpm
.isAdminActive(adminReceiver
))
75 final Intent intent
=new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN
);
76 intent
.putExtra(DevicePolicyManager
.EXTRA_DEVICE_ADMIN
, adminReceiver
);
77 startActivityForResult(intent
, 0);
80 dpm
.removeActiveAdmin(adminReceiver
);
85 final CheckBoxPreference foregroundPreference
=(CheckBoxPreference
) findPreference("foreground");
86 foregroundPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
88 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
89 startService(new Intent(FonBotPreferenceActivity
.this, FonBotMainService
.class));
94 final CheckBoxPreference systemPreference
=(CheckBoxPreference
) findPreference("system");
95 final ApplicationInfo info
=getApplicationInfo();
96 final boolean isSystem
=(info
.flags
&ApplicationInfo
.FLAG_SYSTEM
)!=0;
97 systemPreference
.setChecked(isSystem
);
98 systemPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
100 public boolean onPreferenceChange(final @Nullable Preference preference
, final @Nullable Object newValue
) {
103 final boolean isChecked
=((Boolean
)newValue
).booleanValue();
104 if(isChecked
==isSystem
)
108 final String remountCommand
="mount -o remount,rw /system";
109 final String copyToSystemCommand
="cp "+info
.sourceDir
+" /system/app/FonBot.apk";
110 final String chmodSystemCommand
="chmod 644 /system/app/FonBot.apk";
111 final String copyToUserCommand
="cp "+info
.sourceDir
+" /data/app/FonBot.apk";
112 final String chmodUserCommand
="chmod 644 /data/app/FonBot.apk";
113 final String rmCommand
="rm "+info
.sourceDir
;
116 Runtime
.getRuntime().exec(new String
[]{
118 remountCommand
+';'+copyToSystemCommand
+';'+chmodSystemCommand
+';'+rmCommand
121 Toast
.makeText(FonBotPreferenceActivity
.this,
122 "Reboot to make FonBot a system application", Toast
.LENGTH_LONG
).show();
124 Runtime
.getRuntime().exec(new String
[]{
126 remountCommand
+';'+copyToUserCommand
+';'+chmodUserCommand
+';'+rmCommand
129 Toast
.makeText(FonBotPreferenceActivity
.this,
130 "Reboot to make FonBot a non-system application", Toast
.LENGTH_LONG
).show();
132 } catch (IOException e
) {
134 } catch (InterruptedException e
) {
143 protected void onActivityResult(final int requestCode
, final int resultCode
, @Nullable final Intent data
) {
144 final DevicePolicyManager dpm
=(DevicePolicyManager
) getSystemService(DEVICE_POLICY_SERVICE
);
145 final ComponentName adminReceiver
=new ComponentName(this,FonBotAdminReceiver
.class);
146 adminPreference
.setChecked(dpm
.isAdminActive(adminReceiver
));