From fb1fbc2cafc2902b98ea18ad09200284f967dbb3 Mon Sep 17 00:00:00 2001 From: Tamas Hamor Date: Sat, 3 Nov 2018 14:43:16 +0100 Subject: [PATCH] track history, uninstall instead of hide --- app/src/main/AndroidManifest.xml | 7 +- .../h4mu/sysprotec/AppDetailActivity.java | 45 ++ .../h4mu/sysprotec/AppsHistoryActivity.java | 49 ++ .../sysprotec/InstallBroadcastReceiver.java | 52 +- .../h4mu/sysprotec/LicenseActivity.java | 13 + .../github/h4mu/sysprotec/MainActivity.java | 21 +- .../main/res/layout/activity_app_detail.xml | 48 ++ .../main/res/layout/activity_apps_history.xml | 12 + app/src/main/res/layout/activity_license.xml | 31 + app/src/main/res/layout/activity_main.xml | 19 + app/src/main/res/values/strings.xml | 678 ++++++++++++++++++ 11 files changed, 958 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/io/github/h4mu/sysprotec/AppDetailActivity.java create mode 100644 app/src/main/java/io/github/h4mu/sysprotec/AppsHistoryActivity.java create mode 100644 app/src/main/java/io/github/h4mu/sysprotec/LicenseActivity.java create mode 100644 app/src/main/res/layout/activity_app_detail.xml create mode 100644 app/src/main/res/layout/activity_apps_history.xml create mode 100644 app/src/main/res/layout/activity_license.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d30df4f..0746b2c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,10 +23,10 @@ android:exported="true"> + - + + + + + \ No newline at end of file diff --git a/app/src/main/java/io/github/h4mu/sysprotec/AppDetailActivity.java b/app/src/main/java/io/github/h4mu/sysprotec/AppDetailActivity.java new file mode 100644 index 0000000..3f95c9c --- /dev/null +++ b/app/src/main/java/io/github/h4mu/sysprotec/AppDetailActivity.java @@ -0,0 +1,45 @@ +package io.github.h4mu.sysprotec; + +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.net.Uri; +import android.os.Bundle; +import android.provider.Settings; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.TextView; + +public class AppDetailActivity extends AppCompatActivity { + private ApplicationInfo applicationInfo; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_app_detail); + String packageName = getIntent().getStringExtra(AppsHistoryActivity.COLUMN_PACKAGE); + + try { + applicationInfo = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA); + } catch (PackageManager.NameNotFoundException e) { + ((TextView) findViewById(R.id.nameText)).setText(packageName); + ((TextView) findViewById(R.id.packageText)).setText(packageName); + ((TextView) findViewById(R.id.appDescriptionText)).setText(R.string.not_installed); + return; + } + + findViewById(R.id.button).setEnabled(true); + ((TextView) findViewById(R.id.nameText)).setText(applicationInfo.loadLabel(getPackageManager())); + ((TextView) findViewById(R.id.packageText)).setText(packageName); + ((TextView) findViewById(R.id.appDescriptionText)).setText(applicationInfo.loadDescription(getPackageManager())); + } + + public void onRemoveAppClicked(View view) { + if (applicationInfo != null) { + Intent intent = new Intent(); + intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + intent.setData(Uri.fromParts("package", applicationInfo.packageName, null)); + startActivity(intent); + } + } +} diff --git a/app/src/main/java/io/github/h4mu/sysprotec/AppsHistoryActivity.java b/app/src/main/java/io/github/h4mu/sysprotec/AppsHistoryActivity.java new file mode 100644 index 0000000..4885ed0 --- /dev/null +++ b/app/src/main/java/io/github/h4mu/sysprotec/AppsHistoryActivity.java @@ -0,0 +1,49 @@ +package io.github.h4mu.sysprotec; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListView; +import android.widget.SimpleAdapter; + +import java.util.ArrayList; +import java.util.HashMap; + +public class AppsHistoryActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { + static final String COLUMN_PACKAGE = "package"; + private static final String COLUMN_INSTALLDATE = "install date"; + private ArrayList> apps = new ArrayList<>(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_apps_history); + + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + for (String item : sharedPref.getString("history", "").split(";")) { + String[] pkgDate = item.split("\\|"); + if (pkgDate.length >= 2) { + HashMap row = new HashMap<>(); + row.put(COLUMN_PACKAGE, pkgDate[1]); + row.put(COLUMN_INSTALLDATE, pkgDate[0]); + apps.add(row); + } + } + + ListView listView = findViewById(R.id.packageList); + listView.setAdapter(new SimpleAdapter(this, apps, android.R.layout.simple_list_item_2, + new String[]{COLUMN_INSTALLDATE, COLUMN_PACKAGE}, new int[]{android.R.id.text1, android.R.id.text2})); + listView.setOnItemClickListener(this); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Intent intent = new Intent(AppsHistoryActivity.this, AppDetailActivity.class); + intent.putExtra(COLUMN_PACKAGE, apps.get(position).get(COLUMN_PACKAGE)); + startActivity(intent); + } +} diff --git a/app/src/main/java/io/github/h4mu/sysprotec/InstallBroadcastReceiver.java b/app/src/main/java/io/github/h4mu/sysprotec/InstallBroadcastReceiver.java index d09785c..9da086b 100644 --- a/app/src/main/java/io/github/h4mu/sysprotec/InstallBroadcastReceiver.java +++ b/app/src/main/java/io/github/h4mu/sysprotec/InstallBroadcastReceiver.java @@ -9,40 +9,49 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageInstaller; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.HashSet; import java.util.Set; import static android.content.Context.NOTIFICATION_SERVICE; +import static io.github.h4mu.sysprotec.AppsHistoryActivity.COLUMN_PACKAGE; public class InstallBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); - if (!sharedPref.getBoolean("installDisabled", false)) { + SharedPreferences.Editor editor = sharedPref.edit(); + if (sharedPref.getBoolean("locked", false) && !sharedPref.getBoolean("installDisabled", false)) { + PendingIntent resultPendingIntent; Set packages = sharedPref.getStringSet("packages", new HashSet()); Uri intentData = intent.getData(); if (intentData != null && intentData.getSchemeSpecificPart() != null && !packages.contains(intentData.getSchemeSpecificPart())) { DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName name = new ComponentName(context, AdminReceiver.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && devicePolicyManager.isAdminActive(name)) { - devicePolicyManager.setApplicationHidden(name, intentData.getSchemeSpecificPart(), true); - } + uninstallPackage(context, intentData.getSchemeSpecificPart()); +// devicePolicyManager.setApplicationHidden(name, intentData.getSchemeSpecificPart(), true); + editor.putString("history", sharedPref.getString("history", "") + ";" + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()) + "|" + intentData.getSchemeSpecificPart()); + editor.apply(); - Intent resultIntent = new Intent(Intent.ACTION_DELETE); - resultIntent.setData(intentData); - PendingIntent resultPendingIntent = - PendingIntent.getActivity( - context, - 0, - resultIntent, - PendingIntent.FLAG_UPDATE_CURRENT - ); + Intent AppDetailsIntent = new Intent(context, AppDetailActivity.class); + AppDetailsIntent.putExtra(COLUMN_PACKAGE, intentData.getSchemeSpecificPart()); + resultPendingIntent = PendingIntent.getActivity(context, 0, AppDetailsIntent, PendingIntent.FLAG_UPDATE_CURRENT); + } else { + Intent resultIntent = new Intent(Intent.ACTION_DELETE); + resultIntent.setData(intentData); + resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); + } NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setCategory(Notification.CATEGORY_ERROR) @@ -58,4 +67,23 @@ public void onReceive(Context context, Intent intent) { } } } + + private boolean uninstallPackage(Context context, String packageName) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { + PackageManager packageManger = context.getPackageManager(); + PackageInstaller packageInstaller = packageManger.getPackageInstaller(); + PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL); + params.setAppPackageName(packageName); + int sessionId = 0; + try { + sessionId = packageInstaller.createSession(params); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + packageInstaller.uninstall(packageName, PendingIntent.getBroadcast(context, sessionId, new Intent(Intent.ACTION_MAIN), 0).getIntentSender()); + return true; + } + return false; + } } diff --git a/app/src/main/java/io/github/h4mu/sysprotec/LicenseActivity.java b/app/src/main/java/io/github/h4mu/sysprotec/LicenseActivity.java new file mode 100644 index 0000000..32869f7 --- /dev/null +++ b/app/src/main/java/io/github/h4mu/sysprotec/LicenseActivity.java @@ -0,0 +1,13 @@ +package io.github.h4mu.sysprotec; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +public class LicenseActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_license); + } +} diff --git a/app/src/main/java/io/github/h4mu/sysprotec/MainActivity.java b/app/src/main/java/io/github/h4mu/sysprotec/MainActivity.java index 3358a2e..5b73998 100644 --- a/app/src/main/java/io/github/h4mu/sysprotec/MainActivity.java +++ b/app/src/main/java/io/github/h4mu/sysprotec/MainActivity.java @@ -3,6 +3,7 @@ import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -28,10 +29,14 @@ protected void onCreate(Bundle savedInstanceState) { ((ToggleButton) findViewById(R.id.toggleButton)).setChecked(sharedPref.getBoolean("locked", false)); ToggleButton installDisableToggleButton = findViewById(R.id.installDisableToggleButton); installDisableToggleButton.setChecked(sharedPref.getBoolean("installDisabled", false)); + updateButtonStatuses(); + } + + private void updateButtonStatuses() { DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName name = new ComponentName(this, AdminReceiver.class); boolean isAdmin = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && devicePolicyManager.isAdminActive(name); - installDisableToggleButton.setEnabled(isAdmin); + findViewById(R.id.installDisableToggleButton).setEnabled(isAdmin); findViewById(R.id.disableAdminButton).setEnabled(isAdmin); } @@ -48,9 +53,7 @@ public void toggleClicked(View view) { } editor.putBoolean("locked", button.isChecked()); editor.apply(); - DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); - ComponentName name = new ComponentName(this, AdminReceiver.class); - findViewById(R.id.installDisableToggleButton).setEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && devicePolicyManager.isAdminActive(name)); + updateButtonStatuses(); } public void installDisableClicked(View view) { @@ -68,6 +71,7 @@ public void installDisableClicked(View view) { devicePolicyManager.clearUserRestriction(name, UserManager.DISALLOW_INSTALL_APPS); } } + updateButtonStatuses(); } public void disableAdminClicked(View view) { @@ -83,5 +87,14 @@ public void disableAdminClicked(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && devicePolicyManager.isAdminActive(name)) { devicePolicyManager.clearDeviceOwnerApp(getPackageName()); } + updateButtonStatuses(); + } + + public void onLicenseClicked(View view) { + startActivity(new Intent(this, LicenseActivity.class)); + } + + public void onApplicationsHistoryClicked(View view) { + startActivity(new Intent(this, AppsHistoryActivity.class)); } } diff --git a/app/src/main/res/layout/activity_app_detail.xml b/app/src/main/res/layout/activity_app_detail.xml new file mode 100644 index 0000000..6461991 --- /dev/null +++ b/app/src/main/res/layout/activity_app_detail.xml @@ -0,0 +1,48 @@ + + + + + +