Add notification uris to ApiAppDao
This commit is contained in:
@@ -78,12 +78,16 @@ public class ApiAppDao extends AbstractDao {
|
||||
InsertApiApp statement = new ApiAppsModel.InsertApiApp(getWritableDb());
|
||||
statement.bind(apiApp.package_name(), apiApp.package_signature());
|
||||
statement.executeInsert();
|
||||
|
||||
getDatabaseNotifyManager().notifyApiAppChange(apiApp.package_name());
|
||||
}
|
||||
|
||||
public void deleteApiApp(String packageName) {
|
||||
DeleteByPackageName deleteByPackageName = new DeleteByPackageName(getWritableDb());
|
||||
deleteByPackageName.bind(packageName);
|
||||
deleteByPackageName.executeUpdateDelete();
|
||||
|
||||
getDatabaseNotifyManager().notifyApiAppChange(packageName);
|
||||
}
|
||||
|
||||
public HashSet<Long> getAllowedKeyIdsForApp(String packageName) {
|
||||
@@ -108,12 +112,16 @@ public class ApiAppDao extends AbstractDao {
|
||||
statement.bind(packageName, keyId);
|
||||
statement.execute();
|
||||
}
|
||||
|
||||
getDatabaseNotifyManager().notifyApiAppChange(packageName);
|
||||
}
|
||||
|
||||
public void addAllowedKeyIdForApp(String packageName, long allowedKeyId) {
|
||||
InsertAllowedKey statement = new InsertAllowedKey(getWritableDb());
|
||||
statement.bind(packageName, allowedKeyId);
|
||||
statement.execute();
|
||||
|
||||
getDatabaseNotifyManager().notifyApiAppChange(packageName);
|
||||
}
|
||||
|
||||
public List<ApiApp> getAllApiApps() {
|
||||
|
||||
@@ -9,7 +9,8 @@ import org.sufficientlysecure.keychain.Constants;
|
||||
|
||||
|
||||
public class DatabaseNotifyManager {
|
||||
private static final Uri BASE_URI = Uri.parse("content://" + Constants.PROVIDER_AUTHORITY);
|
||||
private static final Uri URI_KEYS = Uri.parse("content://" + Constants.PROVIDER_AUTHORITY + "/keys");
|
||||
private static final Uri URI_APPS = Uri.parse("content://" + Constants.PROVIDER_AUTHORITY + "/apps");
|
||||
|
||||
private ContentResolver contentResolver;
|
||||
|
||||
@@ -42,11 +43,24 @@ public class DatabaseNotifyManager {
|
||||
contentResolver.notifyChange(uri, null);
|
||||
}
|
||||
|
||||
public void notifyApiAppChange(String apiApp) {
|
||||
Uri uri = getNotifyUriPackageName(apiApp);
|
||||
contentResolver.notifyChange(uri, null);
|
||||
}
|
||||
|
||||
public static Uri getNotifyUriAllKeys() {
|
||||
return BASE_URI;
|
||||
return URI_KEYS;
|
||||
}
|
||||
|
||||
public static Uri getNotifyUriMasterKeyId(long masterKeyId) {
|
||||
return BASE_URI.buildUpon().appendPath(Long.toString(masterKeyId)).build();
|
||||
return URI_KEYS.buildUpon().appendPath(Long.toString(masterKeyId)).build();
|
||||
}
|
||||
|
||||
public static Uri getNotifyUriAllApps() {
|
||||
return URI_APPS;
|
||||
}
|
||||
|
||||
public static Uri getNotifyUriPackageName(String packageName) {
|
||||
return URI_APPS.buildUpon().appendPath(packageName).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package org.sufficientlysecure.keychain.livedata;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.daos.ApiAppDao;
|
||||
import org.sufficientlysecure.keychain.daos.DatabaseNotifyManager;
|
||||
import org.sufficientlysecure.keychain.livedata.ApiAppsLiveData.ListedApp;
|
||||
import org.sufficientlysecure.keychain.model.ApiApp;
|
||||
import org.sufficientlysecure.keychain.ui.keyview.loader.AsyncTaskLiveData;
|
||||
|
||||
|
||||
public class ApiAppsLiveData extends AsyncTaskLiveData<List<ListedApp>> {
|
||||
private final ApiAppDao apiAppDao;
|
||||
private final PackageManager packageManager;
|
||||
|
||||
public ApiAppsLiveData(Context context) {
|
||||
super(context, DatabaseNotifyManager.getNotifyUriAllApps());
|
||||
|
||||
packageManager = getContext().getPackageManager();
|
||||
apiAppDao = ApiAppDao.getInstance(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<ListedApp> asyncLoadData() {
|
||||
ArrayList<ListedApp> result = new ArrayList<>();
|
||||
|
||||
loadRegisteredApps(result);
|
||||
addPlaceholderApps(result);
|
||||
|
||||
Collections.sort(result, (o1, o2) -> o1.readableName.compareTo(o2.readableName));
|
||||
return result;
|
||||
}
|
||||
|
||||
private void loadRegisteredApps(ArrayList<ListedApp> result) {
|
||||
List<ApiApp> registeredApiApps = apiAppDao.getAllApiApps();
|
||||
|
||||
for (ApiApp apiApp : registeredApiApps) {
|
||||
ListedApp listedApp;
|
||||
try {
|
||||
ApplicationInfo ai = packageManager.getApplicationInfo(apiApp.package_name(), 0);
|
||||
CharSequence applicationLabel = packageManager.getApplicationLabel(ai);
|
||||
Drawable applicationIcon = packageManager.getApplicationIcon(ai);
|
||||
|
||||
listedApp = new ListedApp(apiApp.package_name(), true, true, applicationLabel, applicationIcon, null);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
listedApp = new ListedApp(apiApp.package_name(), false, true, apiApp.package_name(), null, null);
|
||||
}
|
||||
result.add(listedApp);
|
||||
}
|
||||
}
|
||||
|
||||
private void addPlaceholderApps(ArrayList<ListedApp> result) {
|
||||
for (ListedApp placeholderApp : PLACERHOLDER_APPS) {
|
||||
if (!containsByPackageName(result, placeholderApp.packageName)) {
|
||||
try {
|
||||
packageManager.getApplicationInfo(placeholderApp.packageName, 0);
|
||||
result.add(placeholderApp.withIsInstalled());
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
result.add(placeholderApp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean containsByPackageName(ArrayList<ListedApp> result, String packageName) {
|
||||
for (ListedApp app : result) {
|
||||
if (packageName.equals(app.packageName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static class ListedApp {
|
||||
public final String packageName;
|
||||
public final boolean isInstalled;
|
||||
public final boolean isRegistered;
|
||||
public final String readableName;
|
||||
public final Drawable applicationIcon;
|
||||
public final Integer applicationIconRes;
|
||||
|
||||
ListedApp(String packageName, boolean isInstalled, boolean isRegistered, CharSequence readableName,
|
||||
Drawable applicationIcon, Integer applicationIconRes) {
|
||||
this.packageName = packageName;
|
||||
this.isInstalled = isInstalled;
|
||||
this.isRegistered = isRegistered;
|
||||
this.readableName = readableName.toString();
|
||||
this.applicationIcon = applicationIcon;
|
||||
this.applicationIconRes = applicationIconRes;
|
||||
}
|
||||
|
||||
public ListedApp withIsInstalled() {
|
||||
return new ListedApp(packageName, true, isRegistered, readableName, applicationIcon, applicationIconRes);
|
||||
}
|
||||
}
|
||||
|
||||
private static final ListedApp[] PLACERHOLDER_APPS = {
|
||||
new ListedApp("com.fsck.k9", false, false, "K-9 Mail", null, R.drawable.apps_k9),
|
||||
new ListedApp("com.zeapo.pwdstore", false, false, "Password Store", null, R.drawable.apps_password_store),
|
||||
new ListedApp("eu.siacs.conversations", false, false, "Conversations (Instant Messaging)", null,
|
||||
R.drawable.apps_conversations)
|
||||
};
|
||||
}
|
||||
@@ -18,18 +18,18 @@
|
||||
package org.sufficientlysecure.keychain.remote.ui;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.ViewModel;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.RecyclerView.Adapter;
|
||||
@@ -40,11 +40,10 @@ import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.model.ApiApp;
|
||||
import org.sufficientlysecure.keychain.daos.ApiAppDao;
|
||||
import org.sufficientlysecure.keychain.livedata.ApiAppsLiveData;
|
||||
import org.sufficientlysecure.keychain.livedata.ApiAppsLiveData.ListedApp;
|
||||
import org.sufficientlysecure.keychain.remote.ui.AppsListFragment.ApiAppAdapter;
|
||||
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
||||
import org.sufficientlysecure.keychain.ui.keyview.loader.AsyncTaskLiveData;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
@@ -61,7 +60,8 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
||||
setAdapter(adapter);
|
||||
setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
|
||||
|
||||
new ApiAppsLiveData(getContext()).observe(this, this::onLoad);
|
||||
ApiAppsViewModel viewModel = ViewModelProviders.of(this).get(ApiAppsViewModel.class);
|
||||
viewModel.getListedAppLiveData(requireContext()).observe(this, this::onLoad);
|
||||
}
|
||||
|
||||
private void onLoad(List<ListedApp> apiApps) {
|
||||
@@ -85,7 +85,7 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
||||
startActivity(intent);
|
||||
} else {
|
||||
Intent i;
|
||||
PackageManager manager = getActivity().getPackageManager();
|
||||
PackageManager manager = requireActivity().getPackageManager();
|
||||
try {
|
||||
i = manager.getLaunchIntentForPackage(listedApp.packageName);
|
||||
if (i == null) {
|
||||
@@ -121,13 +121,14 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
||||
inflater = LayoutInflater.from(context);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ApiAppViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
public ApiAppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new ApiAppViewHolder(inflater.inflate(R.layout.api_apps_adapter_list_item, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ApiAppViewHolder holder, int position) {
|
||||
public void onBindViewHolder(@NonNull ApiAppViewHolder holder, int position) {
|
||||
ListedApp item = data.get(position);
|
||||
holder.bind(item);
|
||||
}
|
||||
@@ -168,97 +169,15 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ApiAppsLiveData extends AsyncTaskLiveData<List<ListedApp>> {
|
||||
private final ApiAppDao apiAppDao;
|
||||
private final PackageManager packageManager;
|
||||
public static class ApiAppsViewModel extends ViewModel {
|
||||
LiveData<List<ListedApp>> listedAppLiveData;
|
||||
|
||||
ApiAppsLiveData(Context context) {
|
||||
super(context, null);
|
||||
|
||||
packageManager = getContext().getPackageManager();
|
||||
apiAppDao = ApiAppDao.getInstance(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<ListedApp> asyncLoadData() {
|
||||
ArrayList<ListedApp> result = new ArrayList<>();
|
||||
|
||||
loadRegisteredApps(result);
|
||||
addPlaceholderApps(result);
|
||||
|
||||
Collections.sort(result, (o1, o2) -> o1.readableName.compareTo(o2.readableName));
|
||||
return result;
|
||||
}
|
||||
|
||||
private void loadRegisteredApps(ArrayList<ListedApp> result) {
|
||||
List<ApiApp> registeredApiApps = apiAppDao.getAllApiApps();
|
||||
|
||||
for (ApiApp apiApp : registeredApiApps) {
|
||||
ListedApp listedApp;
|
||||
try {
|
||||
ApplicationInfo ai = packageManager.getApplicationInfo(apiApp.package_name(), 0);
|
||||
CharSequence applicationLabel = packageManager.getApplicationLabel(ai);
|
||||
Drawable applicationIcon = packageManager.getApplicationIcon(ai);
|
||||
|
||||
listedApp = new ListedApp(apiApp.package_name(), true, true, applicationLabel, applicationIcon, null);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
listedApp = new ListedApp(apiApp.package_name(), false, true, apiApp.package_name(), null, null);
|
||||
}
|
||||
result.add(listedApp);
|
||||
LiveData<List<ListedApp>> getListedAppLiveData(Context context) {
|
||||
if (listedAppLiveData == null) {
|
||||
listedAppLiveData = new ApiAppsLiveData(context);
|
||||
}
|
||||
}
|
||||
|
||||
private void addPlaceholderApps(ArrayList<ListedApp> result) {
|
||||
for (ListedApp placeholderApp : PLACERHOLDER_APPS) {
|
||||
if (!containsByPackageName(result, placeholderApp.packageName)) {
|
||||
try {
|
||||
packageManager.getApplicationInfo(placeholderApp.packageName, 0);
|
||||
result.add(placeholderApp.withIsInstalled());
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
result.add(placeholderApp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean containsByPackageName(ArrayList<ListedApp> result, String packageName) {
|
||||
for (ListedApp app : result) {
|
||||
if (packageName.equals(app.packageName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return listedAppLiveData;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ListedApp {
|
||||
final String packageName;
|
||||
final boolean isInstalled;
|
||||
final boolean isRegistered;
|
||||
final String readableName;
|
||||
final Drawable applicationIcon;
|
||||
final Integer applicationIconRes;
|
||||
|
||||
ListedApp(String packageName, boolean isInstalled, boolean isRegistered, CharSequence readableName,
|
||||
Drawable applicationIcon, Integer applicationIconRes) {
|
||||
this.packageName = packageName;
|
||||
this.isInstalled = isInstalled;
|
||||
this.isRegistered = isRegistered;
|
||||
this.readableName = readableName.toString();
|
||||
this.applicationIcon = applicationIcon;
|
||||
this.applicationIconRes = applicationIconRes;
|
||||
}
|
||||
|
||||
public ListedApp withIsInstalled() {
|
||||
return new ListedApp(packageName, true, isRegistered, readableName, applicationIcon, applicationIconRes);
|
||||
}
|
||||
}
|
||||
|
||||
private static final ListedApp[] PLACERHOLDER_APPS = {
|
||||
new ListedApp("com.fsck.k9", false, false, "K-9 Mail", null, R.drawable.apps_k9),
|
||||
new ListedApp("com.zeapo.pwdstore", false, false, "Password Store", null, R.drawable.apps_password_store),
|
||||
new ListedApp("eu.siacs.conversations", false, false, "Conversations (Instant Messaging)", null,
|
||||
R.drawable.apps_conversations)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user