Skip to content

Commit

Permalink
Fix bugs; add permission manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-yfh committed Feb 6, 2022
1 parent d212f87 commit 1e28fb1
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 7 deletions.
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
android:name=".AppManagerActivity"
android:label="@string/app_manager">
</activity>
<activity
android:name=".PermissionsActivity"
android:label="@string/app_permissions">
</activity>
</application>

</manifest>
45 changes: 39 additions & 6 deletions app/src/main/java/ru/nikita/adb/AppManagerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ListView;
Expand All @@ -25,14 +26,16 @@
import android.util.Log;
import ru.nikita.adb.Device;
import ru.nikita.adb.Task;
import ru.nikita.adb.PermissionsActivity;

public class AppManagerActivity extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sortMode = SORT.TYPE;
adb = (Binary) getIntent().getSerializableExtra("adb");
device = (Device) getIntent().getSerializableExtra("device");
Intent intent = getIntent();
adb = (Binary) intent.getSerializableExtra("adb");
device = (Device) intent.getSerializableExtra("device");
ListView v = getListView();
registerForContextMenu(v);
new AppLoadTask().execute();
Expand Down Expand Up @@ -73,16 +76,27 @@ public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
App app = apps[info.position];
if(id == R.id.app_uninstall_data){
new AppTask().execute(R.string.app_uninstalling, "pm uninstall --user 0 " + app.pkg);
new AppTask().uninstallWithData(app);
return true;
}else if(id == R.id.app_uninstall){
new AppTask().execute(R.string.app_uninstalling, "pm uninstall -k --user 0 " + app.pkg);
new AppTask().uninstall(app);
return true;
}else if(id == R.id.app_install){
new AppTask().execute(R.string.app_installing, "cmd package install-existing " + app.pkg);
new AppTask().install(app);
return true;
}else if(id == R.id.app_clear){
new AppTask().clear(app);
return true;
}else if(id == R.id.app_permissions){
Intent intent = new Intent(this, PermissionsActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("adb", adb);
bundle.putSerializable("device", device);
bundle.putString("package", app.pkg);
intent.putExtras(bundle);
startActivity(intent);
return true;
}

return super.onContextItemSelected(item);
}
private void sortApps(){
Expand Down Expand Up @@ -124,6 +138,7 @@ public int compareTo(App app){
private class AppTask extends ADBTask{
public AppTask(){
super(adb);
update = false;
}
@Override
protected void onPreExecute(){
Expand All @@ -139,13 +154,31 @@ protected void onPostExecute(String log){
log = log.trim();
if(log.length() > 0 && log.length() < 200)
Toast.makeText(getApplicationContext(), log, Toast.LENGTH_SHORT).show();
if(update)
new AppLoadTask().execute();
}
public void execute(int stringId, String args){
this.stringId=stringId;
shell(device, args);
}
public void uninstallWithData(App app){
update = true;
execute(R.string.app_uninstalling, "pm uninstall --user 0 " + app.pkg);
}
public void uninstall(App app){
update = true;
execute(R.string.app_uninstalling, "pm uninstall -k --user 0 " + app.pkg);
}
public void install(App app){
update = true;
execute(R.string.app_installing, "cmd package install-existing " + app.pkg);
}
public void clear(App app){
execute(R.string.app_clearing, "pm clear " + app.pkg);
}

int stringId;
boolean update;
}
private class AppLoadTask extends AppTask{
@Override
Expand Down
162 changes: 162 additions & 0 deletions app/src/main/java/ru/nikita/adb/PermissionsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package ru.nikita.adb;

import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Comparable;
import android.os.Bundle;
import android.os.AsyncTask;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.Toast;
import android.R.layout;
import android.util.SparseBooleanArray;
import ru.nikita.adb.Device;
import ru.nikita.adb.Task;

public class PermissionsActivity extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
adb = (Binary) intent.getSerializableExtra("adb");
device = (Device) intent.getSerializableExtra("device");
pkg = intent.getStringExtra("package");
new AppPermissionLoadTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.permissions_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.permissions_apply){
SparseBooleanArray selected = getListView().getCheckedItemPositions();
boolean granted[] = new boolean[permissions.length];
for(int i = 0; i < granted.length; i++)
granted[i] = selected.get(i);
new AppPermissionsApplyTask().execute(granted);
return true;
}
return false;
}
private class Permission implements Comparable<Permission>{
Permission(String name){
this.name = name;
granted = false;
}
@Override
public int compareTo(Permission p){
return this.name.compareTo(p.name);
}
@Override
public String toString(){
return name;
}
public String name;
public boolean granted;
}
private class AppPermissionLoadTask extends ADBTask{
AppPermissionLoadTask(){
super(adb);
}
@Override
protected void onPreExecute(){
super.onPreExecute();
pd = new ProgressDialog(PermissionsActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage(getResources().getString(R.string.permissions_loading));
pd.show();
}
@Override
protected void onPostExecute(String log){
super.onPostExecute(log);
pd.dismiss();
String[] lines = log.split("\n");
boolean requested = false;
boolean install = false;
ArrayList<Permission>list = new ArrayList<Permission>();
for(String line : lines){
line = line.trim();
if(line.equals("requested permissions:"))
requested = true;
else if(line.equals("install permissions:"))
install = true;
else if(line.contains("User"))
break;
else if(install){
if(!line.contains("REVOKE_ON_UPGRADE")){
String name = line.split(":")[0];
for(Permission p : list)
if(p.name.equals(name))
p.granted = true;
}
}else if(requested)
list.add(new Permission(line));
}
permissions = list.toArray(new Permission[0]);
Arrays.sort(permissions);

setListAdapter(new ArrayAdapter<Permission>(PermissionsActivity.this,
android.R.layout.simple_list_item_multiple_choice, permissions));

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
for(int i = 0; i < permissions.length; i++)
listView.setItemChecked(i, permissions[i].granted);
}

public void execute(){
shell(device, "dumpsys package " + pkg);
}
private ProgressDialog pd;
}
private class AppPermissionsApplyTask extends ADBTask{
AppPermissionsApplyTask(){
super(adb);
}
@Override
protected void onPreExecute(){
super.onPreExecute();
pd = new ProgressDialog(PermissionsActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage(getResources().getString(R.string.app_permissions_applying));
pd.show();
}
@Override
protected void onPostExecute(String log){
super.onPostExecute(log);
pd.dismiss();
if(log.length() > 0)
Toast.makeText(getApplicationContext(), log, Toast.LENGTH_LONG).show();
new AppPermissionLoadTask().execute();
}
public void execute(boolean[] granted){
String command = "";
for(int i = 0; i < permissions.length; i++){
Permission permission = permissions[i];
if(permission.granted != granted[i])
command += String.format("pm %s %s %s; ", granted[i]?"grant":"revoke",
pkg, permission.name);
}
shell(device, command);
}
private ProgressDialog pd;
}


private Binary adb;
private Device device;
private String pkg;

private Permission[] permissions;
}
File renamed without changes
4 changes: 4 additions & 0 deletions app/src/main/res/menu/app_context_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
android:title="@string/app_uninstall" />
<item android:id="@+id/app_install"
android:title="@string/app_install" />
<item android:id="@+id/app_clear"
android:title="@string/app_clear" />
<item android:id="@+id/app_permissions"
android:title="@string/app_permissions" />
</menu>
7 changes: 7 additions & 0 deletions app/src/main/res/menu/permissions_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/permissions_apply"
android:icon="@drawable/apply"
android:showAsAction="always"/>
</menu>
5 changes: 5 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="port">Порт</string>
<string name="app_list">Список приложений</string>
<string name="app_loading">Загрузка списка приложений...</string>
<string name="permissions_loading">Загрузка разрешений...</string>
<string name="show_installed_apps">Только сторонние</string>
<string name="refresh_apps">Обновить список</string>
<string name="file_manager">Менеджер файлов</string>
Expand Down Expand Up @@ -50,8 +51,12 @@
<string name="app_uninstall_data">Удалить с данными</string>
<string name="app_uninstall">Удалить</string>
<string name="app_install">Переустановить</string>
<string name="app_clear">Очистить данные</string>
<string name="app_permissions">Установить разрешения</string>
<string name="app_uninstalling">Удаление...</string>
<string name="app_installing">Установка...</string>
<string name="app_clearing">Очистка данных...</string>
<string name="app_permissions_applying">Применение разрешений...</string>
<string name="command_line">Командная строка</string>
<string name="execute">Выполнить</string>
<string name="sort_name">Сортировка по имени</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="port">Device port</string>
<string name="app_list">App list</string>
<string name="app_loading">Loading applications...</string>
<string name="permissions_loading">Loading permissions...</string>
<string name="show_installed_apps">Show only installed apps</string>
<string name="refresh_apps">Refresh</string>
<string name="file_manager">File manager</string>
Expand Down Expand Up @@ -50,8 +51,12 @@
<string name="app_uninstall_data">Uninstall with data</string>
<string name="app_uninstall">Uninstall without data</string>
<string name="app_install">Install</string>
<string name="app_clear">Clear data</string>
<string name="app_permissions">Set permissions</string>
<string name="app_uninstalling">Uninstalling...</string>
<string name="app_installing">Installing...</string>
<string name="app_clearing">Clearing data...</string>
<string name="app_permissions_applying">Applying permissions...</string>
<string name="command_line">Command line</string>
<string name="execute">Execute</string>
<string name="sort_name">Sort by name</string>
Expand Down

0 comments on commit 1e28fb1

Please sign in to comment.