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());
|
InsertApiApp statement = new ApiAppsModel.InsertApiApp(getWritableDb());
|
||||||
statement.bind(apiApp.package_name(), apiApp.package_signature());
|
statement.bind(apiApp.package_name(), apiApp.package_signature());
|
||||||
statement.executeInsert();
|
statement.executeInsert();
|
||||||
|
|
||||||
|
getDatabaseNotifyManager().notifyApiAppChange(apiApp.package_name());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteApiApp(String packageName) {
|
public void deleteApiApp(String packageName) {
|
||||||
DeleteByPackageName deleteByPackageName = new DeleteByPackageName(getWritableDb());
|
DeleteByPackageName deleteByPackageName = new DeleteByPackageName(getWritableDb());
|
||||||
deleteByPackageName.bind(packageName);
|
deleteByPackageName.bind(packageName);
|
||||||
deleteByPackageName.executeUpdateDelete();
|
deleteByPackageName.executeUpdateDelete();
|
||||||
|
|
||||||
|
getDatabaseNotifyManager().notifyApiAppChange(packageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashSet<Long> getAllowedKeyIdsForApp(String packageName) {
|
public HashSet<Long> getAllowedKeyIdsForApp(String packageName) {
|
||||||
@@ -108,12 +112,16 @@ public class ApiAppDao extends AbstractDao {
|
|||||||
statement.bind(packageName, keyId);
|
statement.bind(packageName, keyId);
|
||||||
statement.execute();
|
statement.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getDatabaseNotifyManager().notifyApiAppChange(packageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAllowedKeyIdForApp(String packageName, long allowedKeyId) {
|
public void addAllowedKeyIdForApp(String packageName, long allowedKeyId) {
|
||||||
InsertAllowedKey statement = new InsertAllowedKey(getWritableDb());
|
InsertAllowedKey statement = new InsertAllowedKey(getWritableDb());
|
||||||
statement.bind(packageName, allowedKeyId);
|
statement.bind(packageName, allowedKeyId);
|
||||||
statement.execute();
|
statement.execute();
|
||||||
|
|
||||||
|
getDatabaseNotifyManager().notifyApiAppChange(packageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ApiApp> getAllApiApps() {
|
public List<ApiApp> getAllApiApps() {
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import org.sufficientlysecure.keychain.Constants;
|
|||||||
|
|
||||||
|
|
||||||
public class DatabaseNotifyManager {
|
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;
|
private ContentResolver contentResolver;
|
||||||
|
|
||||||
@@ -42,11 +43,24 @@ public class DatabaseNotifyManager {
|
|||||||
contentResolver.notifyChange(uri, null);
|
contentResolver.notifyChange(uri, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void notifyApiAppChange(String apiApp) {
|
||||||
|
Uri uri = getNotifyUriPackageName(apiApp);
|
||||||
|
contentResolver.notifyChange(uri, null);
|
||||||
|
}
|
||||||
|
|
||||||
public static Uri getNotifyUriAllKeys() {
|
public static Uri getNotifyUriAllKeys() {
|
||||||
return BASE_URI;
|
return URI_KEYS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Uri getNotifyUriMasterKeyId(long masterKeyId) {
|
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;
|
package org.sufficientlysecure.keychain.remote.ui;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
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.ActivityNotFoundException;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ApplicationInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.support.v7.widget.RecyclerView.Adapter;
|
import android.support.v7.widget.RecyclerView.Adapter;
|
||||||
@@ -40,11 +40,10 @@ import android.widget.ImageView;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.model.ApiApp;
|
import org.sufficientlysecure.keychain.livedata.ApiAppsLiveData;
|
||||||
import org.sufficientlysecure.keychain.daos.ApiAppDao;
|
import org.sufficientlysecure.keychain.livedata.ApiAppsLiveData.ListedApp;
|
||||||
import org.sufficientlysecure.keychain.remote.ui.AppsListFragment.ApiAppAdapter;
|
import org.sufficientlysecure.keychain.remote.ui.AppsListFragment.ApiAppAdapter;
|
||||||
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
||||||
import org.sufficientlysecure.keychain.ui.keyview.loader.AsyncTaskLiveData;
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
||||||
@@ -61,7 +60,8 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
|||||||
setAdapter(adapter);
|
setAdapter(adapter);
|
||||||
setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
|
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) {
|
private void onLoad(List<ListedApp> apiApps) {
|
||||||
@@ -85,7 +85,7 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
|||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} else {
|
} else {
|
||||||
Intent i;
|
Intent i;
|
||||||
PackageManager manager = getActivity().getPackageManager();
|
PackageManager manager = requireActivity().getPackageManager();
|
||||||
try {
|
try {
|
||||||
i = manager.getLaunchIntentForPackage(listedApp.packageName);
|
i = manager.getLaunchIntentForPackage(listedApp.packageName);
|
||||||
if (i == null) {
|
if (i == null) {
|
||||||
@@ -121,13 +121,14 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
|||||||
inflater = LayoutInflater.from(context);
|
inflater = LayoutInflater.from(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
@Override
|
@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));
|
return new ApiAppViewHolder(inflater.inflate(R.layout.api_apps_adapter_list_item, parent, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(ApiAppViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull ApiAppViewHolder holder, int position) {
|
||||||
ListedApp item = data.get(position);
|
ListedApp item = data.get(position);
|
||||||
holder.bind(item);
|
holder.bind(item);
|
||||||
}
|
}
|
||||||
@@ -168,97 +169,15 @@ public class AppsListFragment extends RecyclerFragment<ApiAppAdapter> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ApiAppsLiveData extends AsyncTaskLiveData<List<ListedApp>> {
|
public static class ApiAppsViewModel extends ViewModel {
|
||||||
private final ApiAppDao apiAppDao;
|
LiveData<List<ListedApp>> listedAppLiveData;
|
||||||
private final PackageManager packageManager;
|
|
||||||
|
|
||||||
ApiAppsLiveData(Context context) {
|
LiveData<List<ListedApp>> getListedAppLiveData(Context context) {
|
||||||
super(context, null);
|
if (listedAppLiveData == null) {
|
||||||
|
listedAppLiveData = new ApiAppsLiveData(context);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
return listedAppLiveData;
|
||||||
|
|
||||||
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 {
|
|
||||||
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