Skip to content

Commit

Permalink
track history, uninstall instead of hide
Browse files Browse the repository at this point in the history
  • Loading branch information
h4mu committed Nov 3, 2018
1 parent ea5c9e3 commit fb1fbc2
Show file tree
Hide file tree
Showing 11 changed files with 958 additions and 17 deletions.
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PACKAGE_ADDED" />

<data android:scheme="package" />
</intent-filter>
</receiver>

<receiver
android:name=".AdminReceiver"
android:enabled="true"
Expand All @@ -35,10 +35,15 @@
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>

<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
</receiver>

<activity android:name=".AppsHistoryActivity" />
<activity android:name=".LicenseActivity" />
<activity android:name=".AppDetailActivity"></activity>
</application>

</manifest>
45 changes: 45 additions & 0 deletions app/src/main/java/io/github/h4mu/sysprotec/AppDetailActivity.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<HashMap<String, String>> 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<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> packages = sharedPref.getStringSet("packages", new HashSet<String>());
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)
Expand All @@ -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;
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/io/github/h4mu/sysprotec/LicenseActivity.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 17 additions & 4 deletions app/src/main/java/io/github/h4mu/sysprotec/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -68,6 +71,7 @@ public void installDisableClicked(View view) {
devicePolicyManager.clearUserRestriction(name, UserManager.DISALLOW_INSTALL_APPS);
}
}
updateButtonStatuses();
}

public void disableAdminClicked(View view) {
Expand All @@ -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));
}
}
48 changes: 48 additions & 0 deletions app/src/main/res/layout/activity_app_detail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AppDetailActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:onClick="onRemoveAppClicked"
android:text="@string/removeApp" />

<TextView
android:id="@+id/nameText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />

<TextView
android:id="@+id/packageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead" />

<TextView
android:id="@+id/appDescriptionText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_apps_history.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AppsHistoryActivity">

<ListView
android:id="@+id/packageList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
31 changes: 31 additions & 0 deletions app/src/main/res/layout/activity_license.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LicenseActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
android:scrollbars="vertical"
android:fillViewport="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="@string/licenseText"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Loading

0 comments on commit fb1fbc2

Please sign in to comment.