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
* 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
LoaderManager.LoaderCallbacks<Cursor> {
// This is the Adapter being used to display the list's data.
RegisteredAppsAdapter mAdapter;
AppsAdapter mAdapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
@ -66,7 +65,7 @@ public class AppsListFragment extends ListFragment implements
if (installed) {
if (registered) {
// edit app settings
// Edit app settings
Intent intent = new Intent(getActivity(), AppSettingsActivity.class);
intent.setData(KeychainContract.ApiApps.buildByPackageNameUri(selectedPackageName));
startActivity(intent);
@ -75,9 +74,10 @@ public class AppsListFragment extends ListFragment implements
PackageManager manager = getActivity().getPackageManager();
try {
i = manager.getLaunchIntentForPackage(selectedPackageName);
if (i == null)
if (i == null) {
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.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
@ -91,30 +91,32 @@ public class AppsListFragment extends ListFragment implements
Uri.parse("market://details?id=" + selectedPackageName)));
} catch (ActivityNotFoundException anfe) {
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
// application this would come from a resource.
setEmptyText(getString(R.string.api_no_apps));
// NOTE: No setEmptyText(), we always have the default entries
// We have a menu item to show in action bar.
setHasOptionsMenu(true);
// 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);
// Loader is started in onResume!
// NOTE: Loader is started in onResume!
}
@Override
public void 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);
}
@ -123,7 +125,6 @@ public class AppsListFragment extends ListFragment implements
private static final String TEMP_COLUMN_REGISTERED = "REGISTERED";
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[]{
ApiApps._ID, // 0
ApiApps.PACKAGE_NAME, // 1
@ -149,11 +150,45 @@ public class AppsListFragment extends ListFragment implements
// Now create and return a CursorLoader that will take care of
// 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");
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
setListShown(true);
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
/**
* 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,
@ -237,33 +272,25 @@ public class AppsListFragment extends ListFragment implements
}
}
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(mergedCursor);
return mergedCursor;
}
private int isInstalled(String packageName) {
try {
getActivity().getPackageManager().getApplicationInfo(packageName, 0);
getContext().getPackageManager().getApplicationInfo(packageName, 0);
return 1;
} catch (final PackageManager.NameNotFoundException e) {
return 0;
}
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
private class RegisteredAppsAdapter extends CursorAdapter {
private class AppsAdapter extends CursorAdapter {
private LayoutInflater mInflater;
private PackageManager mPM;
public RegisteredAppsAdapter(Context context, Cursor c, int flags) {
public AppsAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = LayoutInflater.from(context);
@ -273,44 +300,23 @@ public class AppsListFragment extends ListFragment implements
/**
* Similar to CursorAdapter.getItemId().
* Required to build Uris for api apps, which are not based on row ids
*
* @param position
* @return
*/
public String getItemPackageName(int position) {
if (mDataValid && mCursor != null) {
if (mCursor.moveToPosition(position)) {
if (mDataValid && mCursor != null && mCursor.moveToPosition(position)) {
return mCursor.getString(INDEX_PACKAGE_NAME);
} else {
return null;
}
} else {
return null;
}
}
public boolean getItemIsInstalled(int position) {
if (mDataValid && mCursor != null) {
if (mCursor.moveToPosition(position)) {
return (mCursor.getInt(INDEX_INSTALLED) == 1);
} else {
return false;
}
} else {
return false;
}
return mDataValid && mCursor != null
&& mCursor.moveToPosition(position) && (mCursor.getInt(INDEX_INSTALLED) == 1);
}
public boolean getItemIsRegistered(int position) {
if (mDataValid && mCursor != null) {
if (mCursor.moveToPosition(position)) {
return (mCursor.getInt(INDEX_REGISTERED) == 1);
} else {
return false;
}
} else {
return false;
}
return mDataValid && mCursor != null
&& mCursor.moveToPosition(position) && (mCursor.getInt(INDEX_REGISTERED) == 1);
}
@Override

View File

@ -476,7 +476,6 @@
<string name="intent_send_decrypt">"Decrypt with OpenKeychain"</string>
<!-- 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_hide_info">"Hide advanced information"</string>
<string name="api_settings_show_advanced">"Show advanced settings"</string>