Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Option to select applications
Browse files Browse the repository at this point in the history
Refs #686
  • Loading branch information
M66B committed Nov 14, 2013
1 parent fedcd11 commit 506e7e6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
16 changes: 10 additions & 6 deletions res/menu/app.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/menu_help"
android:title="@string/menu_help"
android:icon="?attr/icon_help"
android:showAsAction="always" />
<item
android:id="@+id/menu_help"
android:icon="?attr/icon_help"
android:showAsAction="always"
android:title="@string/menu_help"/>
<item
android:id="@+id/menu_apply"
android:title="@string/menu_apply"/>
Expand Down Expand Up @@ -40,11 +41,14 @@
<item
android:id="@+id/menu_accounts"
android:title="@string/menu_accounts"/>
<item
android:id="@+id/menu_applications"
android:title="@string/menu_applications"/>
<item
android:id="@+id/menu_contacts"
android:title="@string/menu_contacts"/>
<item
android:id="@+id/menu_settings"
android:title="@string/menu_settings"/>
</menu>

</menu>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="menu_app_settings">App details</string>
<string name="menu_app_store">Play Store</string>
<string name="menu_accounts">Select accounts to allow</string>
<string name="menu_applications">Select applications to allow</string>
<string name="menu_contacts">Select contacts to allow</string>
<string name="msg_new">New</string>
<string name="msg_update">Update</string>
Expand Down
80 changes: 80 additions & 0 deletions src/biz/bokhorst/xprivacy/ActivityApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -37,6 +39,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
Expand Down Expand Up @@ -245,9 +248,12 @@ public boolean onPrepareOptionsMenu(Menu menu) {
// Accounts
boolean accountsRestricted = PrivacyManager.getRestricted(null, this, mAppInfo.getUid(),
PrivacyManager.cAccounts, null, false, false);
boolean appsRestricted = PrivacyManager.getRestricted(null, this, mAppInfo.getUid(), PrivacyManager.cSystem,
null, false, false);
boolean contactsRestricted = PrivacyManager.getRestricted(null, this, mAppInfo.getUid(),
PrivacyManager.cContacts, null, false, false);
menu.findItem(R.id.menu_accounts).setEnabled(accountsRestricted);
menu.findItem(R.id.menu_applications).setEnabled(appsRestricted);
menu.findItem(R.id.menu_contacts).setEnabled(contactsRestricted);

return super.onPrepareOptionsMenu(menu);
Expand Down Expand Up @@ -294,6 +300,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.menu_accounts:
optionAccounts();
return true;
case R.id.menu_applications:
optionApplications();
return true;
case R.id.menu_contacts:
optionContacts();
return true;
Expand Down Expand Up @@ -436,6 +445,11 @@ private void optionAccounts() {
accountsTask.executeOnExecutor(mExecutor, (Object) null);
}

private void optionApplications() {
ApplicationsTask appsTask = new ApplicationsTask();
appsTask.executeOnExecutor(mExecutor, (Object) null);
}

private void optionLaunch() {
Intent intentLaunch = getPackageManager().getLaunchIntentForPackage(mAppInfo.getPackageName());
startActivity(intentLaunch);
Expand Down Expand Up @@ -521,6 +535,72 @@ public void onClick(DialogInterface dialog, int which) {
}
}

private class ApplicationsTask extends AsyncTask<Object, Object, Object> {
private List<ApplicationInfo> mListInfo;
private List<CharSequence> mListApp;
private boolean[] mSelection;

@Override
protected Object doInBackground(Object... params) {
// Get applications
final PackageManager pm = ActivityApp.this.getPackageManager();
mListInfo = pm.getInstalledApplications(PackageManager.GET_META_DATA);
Collections.sort(mListInfo, new Comparator<ApplicationInfo>() {
public int compare(ApplicationInfo info1, ApplicationInfo info2) {
return ((String) pm.getApplicationLabel(info1)).compareTo(((String) pm.getApplicationLabel(info2)));
}
});

// Build selection list
mListApp = new ArrayList<CharSequence>();
mSelection = new boolean[mListInfo.size()];
for (int i = 0; i < mListInfo.size(); i++)
try {
mListApp.add(String.format("%s (%s)", pm.getApplicationLabel(mListInfo.get(i)),
mListInfo.get(i).packageName));
mSelection[i] = PrivacyManager.getSettingBool(null, ActivityApp.this, 0,
String.format("Application.%s", mListInfo.get(i).packageName), false, false);
} catch (Throwable ex) {
Util.bug(null, ex);
}
return null;
}

@Override
protected void onPostExecute(Object result) {
// Build dialog
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ActivityApp.this);
alertDialogBuilder.setTitle(getString(R.string.menu_applications));
alertDialogBuilder.setIcon(getThemed(R.attr.icon_launcher));
alertDialogBuilder.setMultiChoiceItems(mListApp.toArray(new CharSequence[0]), mSelection,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
try {
PrivacyManager.setSetting(null, ActivityApp.this, 0,
String.format("Application.%s", mListInfo.get(whichButton).packageName),
Boolean.toString(isChecked));
} catch (Throwable ex) {
Util.bug(null, ex);
Toast toast = Toast.makeText(ActivityApp.this, ex.toString(), Toast.LENGTH_LONG);
toast.show();
}
}
});
alertDialogBuilder.setPositiveButton(getString(R.string.msg_done), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});

// Show dialog
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

super.onPostExecute(result);
}
}

private class ContactsTask extends AsyncTask<Object, Object, Object> {
private List<CharSequence> mListContact;
private long[] mIds;
Expand Down

0 comments on commit 506e7e6

Please sign in to comment.