]>
Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
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.Preference; | |
14 | import android.preference.Preference.OnPreferenceChangeListener; | |
15 | import android.preference.PreferenceActivity; | |
16 | import android.util.Log; | |
17 | import android.widget.Toast; | |
18 | ||
19 | /* | |
20 | * Copyright © 2013 Marius Gavrilescu | |
21 | * | |
22 | * This file is part of FonBot. | |
23 | * | |
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. | |
28 | * | |
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. | |
33 | * | |
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/>. | |
36 | */ | |
37 | ||
38 | /** | |
39 | * Preference activity. Lets the user change app preferences. | |
40 | * | |
41 | * @author Marius Gavrilescu <marius@ieval.ro> | |
42 | */ | |
43 | public final class FonBotPreferenceActivity extends PreferenceActivity { | |
44 | /** | |
45 | * Preference for toggling device admin permissions. | |
46 | */ | |
47 | private CheckBoxPreference adminPreference; | |
48 | ||
49 | @SuppressWarnings("deprecation") | |
50 | @Override | |
51 | protected void onCreate(@Nullable final Bundle icicle) { | |
52 | super.onCreate(icicle); | |
53 | setTitle("FonBot Preferences"); | |
54 | addPreferencesFromResource(R.xml.prefs); | |
55 | ||
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)); | |
60 | ||
61 | adminPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
62 | ||
63 | @Override | |
64 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
65 | if(newValue==null){ | |
66 | Log.wtf(FonBotPreferenceActivity.class.getName(), "newValue in OnPreferenceChange is null"); | |
67 | throw new AssertionError("Log.wtf did not terminate the process"); | |
68 | } | |
69 | ||
70 | final boolean isChecked=((Boolean) newValue).booleanValue(); | |
71 | if(isChecked){ | |
72 | if(dpm.isAdminActive(adminReceiver)) | |
73 | return true; | |
74 | ||
75 | final Intent intent=new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); | |
76 | intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver); | |
77 | startActivityForResult(intent, 0); | |
78 | return false; | |
79 | } | |
80 | dpm.removeActiveAdmin(adminReceiver); | |
81 | return true; | |
82 | } | |
83 | }); | |
84 | ||
85 | final CheckBoxPreference foregroundPreference=(CheckBoxPreference) findPreference("foreground"); | |
86 | foregroundPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { | |
87 | @Override | |
88 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
89 | startService(new Intent(FonBotPreferenceActivity.this, FonBotMainService.class)); | |
90 | return true; | |
91 | } | |
92 | }); | |
93 | ||
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() { | |
99 | @Override | |
100 | public boolean onPreferenceChange(final @Nullable Preference preference, final @Nullable Object newValue) { | |
101 | if(newValue==null) | |
102 | return false; | |
103 | final boolean isChecked=((Boolean)newValue).booleanValue(); | |
104 | if(isChecked==isSystem) | |
105 | return true; | |
106 | ||
107 | try { | |
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; | |
114 | ||
115 | if(isChecked){ | |
116 | Runtime.getRuntime().exec(new String[]{ | |
117 | "su", "-c", | |
118 | remountCommand+';'+copyToSystemCommand+';'+chmodSystemCommand+';'+rmCommand | |
119 | }).waitFor(); | |
120 | ||
121 | Toast.makeText(FonBotPreferenceActivity.this, | |
122 | "Reboot to make FonBot a system application", Toast.LENGTH_LONG).show(); | |
123 | } else { | |
124 | Runtime.getRuntime().exec(new String[]{ | |
125 | "su", "-c", | |
126 | remountCommand+';'+copyToUserCommand+';'+chmodUserCommand+';'+rmCommand | |
127 | }).waitFor(); | |
128 | ||
129 | Toast.makeText(FonBotPreferenceActivity.this, | |
130 | "Reboot to make FonBot a non-system application", Toast.LENGTH_LONG).show(); | |
131 | } | |
132 | } catch (IOException e) { | |
133 | return false; | |
134 | } catch (InterruptedException e) { | |
135 | return false; | |
136 | } | |
137 | return true; | |
138 | } | |
139 | }); | |
140 | } | |
141 | ||
142 | @Override | |
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)); | |
147 | } | |
148 | ||
149 | } |