Refactor AppsListFragment

This commit is contained in:
Dominik Schürmann 2015-02-23 21:52:04 +01:00
parent dd3af50956
commit 2064d81aef
2 changed files with 142 additions and 137 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2013-2014 Dominik Schürmann <dominik@dominikschuermann.de> * Copyright (C) 2013-2015 Dominik Schürmann <dominik@dominikschuermann.de>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -50,8 +50,7 @@ import org.sufficientlysecure.keychain.util.Log;
public class AppsListFragment extends ListFragment implements public class AppsListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor> {
// This is the Adapter being used to display the list's data. AppsAdapter mAdapter;
RegisteredAppsAdapter mAdapter;
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
@ -66,7 +65,7 @@ public class AppsListFragment extends ListFragment implements
if (installed) { if (installed) {
if (registered) { if (registered) {
// edit app settings // Edit app settings
Intent intent = new Intent(getActivity(), AppSettingsActivity.class); Intent intent = new Intent(getActivity(), AppSettingsActivity.class);
intent.setData(KeychainContract.ApiApps.buildByPackageNameUri(selectedPackageName)); intent.setData(KeychainContract.ApiApps.buildByPackageNameUri(selectedPackageName));
startActivity(intent); startActivity(intent);
@ -75,9 +74,10 @@ public class AppsListFragment extends ListFragment implements
PackageManager manager = getActivity().getPackageManager(); PackageManager manager = getActivity().getPackageManager();
try { try {
i = manager.getLaunchIntentForPackage(selectedPackageName); i = manager.getLaunchIntentForPackage(selectedPackageName);
if (i == null) if (i == null) {
throw new PackageManager.NameNotFoundException(); throw new PackageManager.NameNotFoundException();
// start like the Android launcher would do }
// Start like the Android launcher would do
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.addCategory(Intent.CATEGORY_LAUNCHER); i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i); startActivity(i);
@ -91,30 +91,32 @@ public class AppsListFragment extends ListFragment implements
Uri.parse("market://details?id=" + selectedPackageName))); Uri.parse("market://details?id=" + selectedPackageName)));
} catch (ActivityNotFoundException anfe) { } catch (ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + selectedPackageName))); Uri.parse("https://play.google.com/store/apps/details?id=" + selectedPackageName)));
} }
} }
} }
}); });
// Give some text to display if there is no data. In a real // NOTE: No setEmptyText(), we always have the default entries
// application this would come from a resource.
setEmptyText(getString(R.string.api_no_apps));
// We have a menu item to show in action bar. // We have a menu item to show in action bar.
setHasOptionsMenu(true); setHasOptionsMenu(true);
// Create an empty adapter we will use to display the loaded data. // Create an empty adapter we will use to display the loaded data.
mAdapter = new RegisteredAppsAdapter(getActivity(), null, 0); mAdapter = new AppsAdapter(getActivity(), null, 0);
setListAdapter(mAdapter); setListAdapter(mAdapter);
// Loader is started in onResume! // NOTE: Loader is started in onResume!
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
// after coming back from Google Play -> reload
// Start out with a progress indicator.
setListShown(false);
// After coming back from Google Play -> reload
getLoaderManager().restartLoader(0, null, this); getLoaderManager().restartLoader(0, null, this);
} }
@ -123,7 +125,6 @@ public class AppsListFragment extends ListFragment implements
private static final String TEMP_COLUMN_REGISTERED = "REGISTERED"; private static final String TEMP_COLUMN_REGISTERED = "REGISTERED";
private static final String TEMP_COLUMN_ICON_RES_ID = "ICON_RES_ID"; private static final String TEMP_COLUMN_ICON_RES_ID = "ICON_RES_ID";
// These are the Contacts rows that we will retrieve.
static final String[] PROJECTION = new String[]{ static final String[] PROJECTION = new String[]{
ApiApps._ID, // 0 ApiApps._ID, // 0
ApiApps.PACKAGE_NAME, // 1 ApiApps.PACKAGE_NAME, // 1
@ -149,106 +150,17 @@ public class AppsListFragment extends ListFragment implements
// Now create and return a CursorLoader that will take care of // Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed. // creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), baseUri, PROJECTION, null, null, return new AppsLoader(getActivity(), baseUri, PROJECTION, null, null,
ApiApps.PACKAGE_NAME + " COLLATE LOCALIZED ASC"); ApiApps.PACKAGE_NAME + " COLLATE LOCALIZED ASC");
} }
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
MatrixCursor availableAppsCursor = new MatrixCursor(new String[]{
ApiApps._ID,
ApiApps.PACKAGE_NAME,
TEMP_COLUMN_NAME,
TEMP_COLUMN_INSTALLED,
TEMP_COLUMN_REGISTERED,
TEMP_COLUMN_ICON_RES_ID
});
// NOTE: SORT ascending by package name, this is REQUIRED for CursorJoiner!
// Drawables taken from projects res/drawables-xxhdpi/ic_launcher.png
availableAppsCursor.addRow(new Object[]{1, "com.fsck.k9", "K-9 Mail", 0, 0, R.drawable.apps_k9});
availableAppsCursor.addRow(new Object[]{1, "com.zeapo.pwdstore", "Password Store", 0, 0, R.drawable.apps_password_store});
availableAppsCursor.addRow(new Object[]{1, "eu.siacs.conversations", "Conversations (Instant Messaging)", 0, 0, R.drawable.apps_conversations});
MatrixCursor mergedCursor = new MatrixCursor(new String[]{
ApiApps._ID,
ApiApps.PACKAGE_NAME,
TEMP_COLUMN_NAME,
TEMP_COLUMN_INSTALLED,
TEMP_COLUMN_REGISTERED,
TEMP_COLUMN_ICON_RES_ID
});
CursorJoiner joiner = new CursorJoiner(
availableAppsCursor,
new String[]{ApiApps.PACKAGE_NAME},
data,
new String[]{ApiApps.PACKAGE_NAME});
for (CursorJoiner.Result joinerResult : joiner) {
switch (joinerResult) {
case LEFT: {
// handle case where a row in availableAppsCursor is unique
String packageName = availableAppsCursor.getString(INDEX_PACKAGE_NAME);
mergedCursor.addRow(new Object[]{
1, // no need for unique _ID
packageName,
availableAppsCursor.getString(INDEX_NAME),
isInstalled(packageName),
0,
availableAppsCursor.getInt(INDEX_ICON_RES_ID)
});
break;
}
case RIGHT: {
// handle case where a row in data is unique
String packageName = data.getString(INDEX_PACKAGE_NAME);
mergedCursor.addRow(new Object[]{
1, // no need for unique _ID
packageName,
null,
isInstalled(packageName),
1, // registered!
R.drawable.ic_launcher // icon is retrieved later
});
break;
}
case BOTH: {
// handle case where a row with the same key is in both cursors
String packageName = data.getString(INDEX_PACKAGE_NAME);
String name;
if (isInstalled(packageName) == 1) {
name = data.getString(INDEX_NAME);
} else {
// if not installed take name from available apps list
name = availableAppsCursor.getString(INDEX_NAME);
}
mergedCursor.addRow(new Object[]{
1, // no need for unique _ID
packageName,
name,
isInstalled(packageName),
1, // registered!
R.drawable.ic_launcher // icon is retrieved later
});
break;
}
}
}
// Swap the new cursor in. (The framework will take care of closing the // Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.) // old cursor once we return.)
mAdapter.swapCursor(mergedCursor); mAdapter.swapCursor(data);
}
private int isInstalled(String packageName) { // The list should now be shown.
try { setListShown(true);
getActivity().getPackageManager().getApplicationInfo(packageName, 0);
return 1;
} catch (final PackageManager.NameNotFoundException e) {
return 0;
}
} }
public void onLoaderReset(Loader<Cursor> loader) { public void onLoaderReset(Loader<Cursor> loader) {
@ -258,12 +170,127 @@ public class AppsListFragment extends ListFragment implements
mAdapter.swapCursor(null); mAdapter.swapCursor(null);
} }
private class RegisteredAppsAdapter extends CursorAdapter { /**
* Besides the queried cursor with all registered apps, this loader also returns non-installed
* proposed apps using a MatrixCursor.
*/
private static class AppsLoader extends CursorLoader {
public AppsLoader(Context context) {
super(context);
}
public AppsLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
super(context, uri, projection, selection, selectionArgs, sortOrder);
}
@Override
public Cursor loadInBackground() {
// Load registered apps from content provider
Cursor data = super.loadInBackground();
MatrixCursor availableAppsCursor = new MatrixCursor(new String[]{
ApiApps._ID,
ApiApps.PACKAGE_NAME,
TEMP_COLUMN_NAME,
TEMP_COLUMN_INSTALLED,
TEMP_COLUMN_REGISTERED,
TEMP_COLUMN_ICON_RES_ID
});
// NOTE: SORT ascending by package name, this is REQUIRED for CursorJoiner!
// Drawables taken from projects res/drawables-xxhdpi/ic_launcher.png
availableAppsCursor.addRow(new Object[]{1, "com.fsck.k9", "K-9 Mail", 0, 0, R.drawable.apps_k9});
availableAppsCursor.addRow(new Object[]{1, "com.zeapo.pwdstore", "Password Store", 0, 0, R.drawable.apps_password_store});
availableAppsCursor.addRow(new Object[]{1, "eu.siacs.conversations", "Conversations (Instant Messaging)", 0, 0, R.drawable.apps_conversations});
MatrixCursor mergedCursor = new MatrixCursor(new String[]{
ApiApps._ID,
ApiApps.PACKAGE_NAME,
TEMP_COLUMN_NAME,
TEMP_COLUMN_INSTALLED,
TEMP_COLUMN_REGISTERED,
TEMP_COLUMN_ICON_RES_ID
});
CursorJoiner joiner = new CursorJoiner(
availableAppsCursor,
new String[]{ApiApps.PACKAGE_NAME},
data,
new String[]{ApiApps.PACKAGE_NAME});
for (CursorJoiner.Result joinerResult : joiner) {
switch (joinerResult) {
case LEFT: {
// handle case where a row in availableAppsCursor is unique
String packageName = availableAppsCursor.getString(INDEX_PACKAGE_NAME);
mergedCursor.addRow(new Object[]{
1, // no need for unique _ID
packageName,
availableAppsCursor.getString(INDEX_NAME),
isInstalled(packageName),
0,
availableAppsCursor.getInt(INDEX_ICON_RES_ID)
});
break;
}
case RIGHT: {
// handle case where a row in data is unique
String packageName = data.getString(INDEX_PACKAGE_NAME);
mergedCursor.addRow(new Object[]{
1, // no need for unique _ID
packageName,
null,
isInstalled(packageName),
1, // registered!
R.drawable.ic_launcher // icon is retrieved later
});
break;
}
case BOTH: {
// handle case where a row with the same key is in both cursors
String packageName = data.getString(INDEX_PACKAGE_NAME);
String name;
if (isInstalled(packageName) == 1) {
name = data.getString(INDEX_NAME);
} else {
// if not installed take name from available apps list
name = availableAppsCursor.getString(INDEX_NAME);
}
mergedCursor.addRow(new Object[]{
1, // no need for unique _ID
packageName,
name,
isInstalled(packageName),
1, // registered!
R.drawable.ic_launcher // icon is retrieved later
});
break;
}
}
}
return mergedCursor;
}
private int isInstalled(String packageName) {
try {
getContext().getPackageManager().getApplicationInfo(packageName, 0);
return 1;
} catch (final PackageManager.NameNotFoundException e) {
return 0;
}
}
}
private class AppsAdapter extends CursorAdapter {
private LayoutInflater mInflater; private LayoutInflater mInflater;
private PackageManager mPM; private PackageManager mPM;
public RegisteredAppsAdapter(Context context, Cursor c, int flags) { public AppsAdapter(Context context, Cursor c, int flags) {
super(context, c, flags); super(context, c, flags);
mInflater = LayoutInflater.from(context); mInflater = LayoutInflater.from(context);
@ -273,44 +300,23 @@ public class AppsListFragment extends ListFragment implements
/** /**
* Similar to CursorAdapter.getItemId(). * Similar to CursorAdapter.getItemId().
* Required to build Uris for api apps, which are not based on row ids * Required to build Uris for api apps, which are not based on row ids
*
* @param position
* @return
*/ */
public String getItemPackageName(int position) { public String getItemPackageName(int position) {
if (mDataValid && mCursor != null) { if (mDataValid && mCursor != null && mCursor.moveToPosition(position)) {
if (mCursor.moveToPosition(position)) { return mCursor.getString(INDEX_PACKAGE_NAME);
return mCursor.getString(INDEX_PACKAGE_NAME);
} else {
return null;
}
} else { } else {
return null; return null;
} }
} }
public boolean getItemIsInstalled(int position) { public boolean getItemIsInstalled(int position) {
if (mDataValid && mCursor != null) { return mDataValid && mCursor != null
if (mCursor.moveToPosition(position)) { && mCursor.moveToPosition(position) && (mCursor.getInt(INDEX_INSTALLED) == 1);
return (mCursor.getInt(INDEX_INSTALLED) == 1);
} else {
return false;
}
} else {
return false;
}
} }
public boolean getItemIsRegistered(int position) { public boolean getItemIsRegistered(int position) {
if (mDataValid && mCursor != null) { return mDataValid && mCursor != null
if (mCursor.moveToPosition(position)) { && mCursor.moveToPosition(position) && (mCursor.getInt(INDEX_REGISTERED) == 1);
return (mCursor.getInt(INDEX_REGISTERED) == 1);
} else {
return false;
}
} else {
return false;
}
} }
@Override @Override

View File

@ -476,7 +476,6 @@
<string name="intent_send_decrypt">"Decrypt with OpenKeychain"</string> <string name="intent_send_decrypt">"Decrypt with OpenKeychain"</string>
<!-- Remote API --> <!-- Remote API -->
<string name="api_no_apps">"No registered apps!\n\nA list of supported third-party applications can be found in 'Help'!"</string>
<string name="api_settings_show_info">"Show advanced information"</string> <string name="api_settings_show_info">"Show advanced information"</string>
<string name="api_settings_hide_info">"Hide advanced information"</string> <string name="api_settings_hide_info">"Hide advanced information"</string>
<string name="api_settings_show_advanced">"Show advanced settings"</string> <string name="api_settings_show_advanced">"Show advanced settings"</string>