2012-10-30 19:39:32 -04:00
|
|
|
/*
|
2014-01-01 16:26:19 -05:00
|
|
|
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
|
2012-10-30 19:39:32 -04:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2012-10-30 19:39:32 -04:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-10-30 19:39:32 -04:00
|
|
|
*
|
2013-09-06 10:17:01 -04:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-10-30 19:39:32 -04:00
|
|
|
*/
|
|
|
|
|
2013-01-16 08:31:16 -05:00
|
|
|
package org.sufficientlysecure.keychain.ui;
|
2012-10-28 18:51:03 -04:00
|
|
|
|
2014-01-05 18:58:04 -05:00
|
|
|
import java.util.Set;
|
|
|
|
|
2013-01-16 08:31:16 -05:00
|
|
|
import org.sufficientlysecure.keychain.Id;
|
2014-01-01 16:26:19 -05:00
|
|
|
import org.sufficientlysecure.keychain.R;
|
2014-01-01 10:54:55 -05:00
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
2013-01-16 08:31:16 -05:00
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
|
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;
|
2014-01-09 09:12:41 -05:00
|
|
|
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
2014-01-01 16:26:19 -05:00
|
|
|
import org.sufficientlysecure.keychain.ui.adapter.KeyListPublicAdapter;
|
2014-01-05 18:58:04 -05:00
|
|
|
import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment;
|
2012-10-28 18:51:03 -04:00
|
|
|
|
2014-01-02 15:10:08 -05:00
|
|
|
import se.emilsjolander.stickylistheaders.ApiLevelTooLowException;
|
|
|
|
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
|
2014-02-04 09:01:50 -05:00
|
|
|
|
2014-01-02 15:10:08 -05:00
|
|
|
import android.annotation.SuppressLint;
|
2012-10-30 19:39:32 -04:00
|
|
|
import android.content.Intent;
|
2012-10-28 18:51:03 -04:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2014-01-05 18:58:04 -05:00
|
|
|
import android.os.Build;
|
2012-10-28 18:51:03 -04:00
|
|
|
import android.os.Bundle;
|
2014-01-05 18:58:04 -05:00
|
|
|
import android.support.v4.app.Fragment;
|
2014-01-01 16:26:19 -05:00
|
|
|
import android.support.v4.app.LoaderManager;
|
2012-10-28 18:51:03 -04:00
|
|
|
import android.support.v4.content.CursorLoader;
|
|
|
|
import android.support.v4.content.Loader;
|
2014-01-05 18:58:04 -05:00
|
|
|
import android.view.ActionMode;
|
2014-01-02 15:10:08 -05:00
|
|
|
import android.view.LayoutInflater;
|
2014-01-05 18:58:04 -05:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2012-10-30 19:39:32 -04:00
|
|
|
import android.view.View;
|
2014-01-07 17:48:16 -05:00
|
|
|
import android.view.View.OnClickListener;
|
2014-01-09 09:12:41 -05:00
|
|
|
import android.view.ViewGroup;
|
2014-01-05 18:58:04 -05:00
|
|
|
import android.widget.AbsListView.MultiChoiceModeListener;
|
2014-01-01 16:26:19 -05:00
|
|
|
import android.widget.AdapterView;
|
2014-01-05 18:58:04 -05:00
|
|
|
import android.widget.ListView;
|
2014-01-02 15:10:08 -05:00
|
|
|
|
2014-01-09 09:12:41 -05:00
|
|
|
import com.beardedhen.androidbootstrap.BootstrapButton;
|
|
|
|
|
2014-01-02 15:10:08 -05:00
|
|
|
/**
|
2014-01-03 12:07:42 -05:00
|
|
|
* Public key list with sticky list headers. It does _not_ extend ListFragment because it uses
|
|
|
|
* StickyListHeaders library which does not extend upon ListView.
|
2014-01-02 15:10:08 -05:00
|
|
|
*/
|
2014-01-05 18:58:04 -05:00
|
|
|
public class KeyListPublicFragment extends Fragment implements AdapterView.OnItemClickListener,
|
|
|
|
LoaderManager.LoaderCallbacks<Cursor> {
|
2012-10-28 18:51:03 -04:00
|
|
|
|
2014-01-01 16:26:19 -05:00
|
|
|
private KeyListPublicAdapter mAdapter;
|
2014-01-02 16:36:57 -05:00
|
|
|
private StickyListHeadersListView mStickyList;
|
2012-10-28 18:51:03 -04:00
|
|
|
|
2014-02-04 09:01:50 -05:00
|
|
|
// empty list layout
|
2014-01-09 09:12:41 -05:00
|
|
|
private BootstrapButton mButtonEmptyCreate;
|
|
|
|
private BootstrapButton mButtonEmptyImport;
|
2014-01-07 17:48:16 -05:00
|
|
|
|
2014-01-02 16:36:57 -05:00
|
|
|
/**
|
|
|
|
* Load custom layout with StickyListView from library
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.key_list_public_fragment, container, false);
|
2014-01-07 17:48:16 -05:00
|
|
|
|
2014-01-09 09:12:41 -05:00
|
|
|
mButtonEmptyCreate = (BootstrapButton) view.findViewById(R.id.key_list_empty_button_create);
|
2014-01-07 17:48:16 -05:00
|
|
|
mButtonEmptyCreate.setOnClickListener(new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(getActivity(), EditKeyActivity.class);
|
|
|
|
intent.setAction(EditKeyActivity.ACTION_CREATE_KEY);
|
|
|
|
intent.putExtra(EditKeyActivity.EXTRA_GENERATE_DEFAULT_KEYS, true);
|
|
|
|
intent.putExtra(EditKeyActivity.EXTRA_USER_IDS, ""); // show user id view
|
|
|
|
startActivityForResult(intent, 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-09 09:12:41 -05:00
|
|
|
mButtonEmptyImport = (BootstrapButton) view.findViewById(R.id.key_list_empty_button_import);
|
2014-01-07 17:48:16 -05:00
|
|
|
mButtonEmptyImport.setOnClickListener(new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2014-02-04 09:01:50 -05:00
|
|
|
Intent intent = new Intent(getActivity(), ImportKeysActivity.class);
|
|
|
|
intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE);
|
|
|
|
startActivityForResult(intent, 0);
|
2014-01-07 17:48:16 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-02 16:36:57 -05:00
|
|
|
return view;
|
|
|
|
}
|
2014-01-02 15:10:08 -05:00
|
|
|
|
2012-10-28 18:51:03 -04:00
|
|
|
/**
|
|
|
|
* Define Adapter and Loader on create of Activity
|
|
|
|
*/
|
2014-01-02 15:10:08 -05:00
|
|
|
@SuppressLint("NewApi")
|
2012-10-28 18:51:03 -04:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
2014-01-02 16:36:57 -05:00
|
|
|
mStickyList = (StickyListHeadersListView) getActivity().findViewById(R.id.list);
|
2012-10-28 18:51:03 -04:00
|
|
|
|
2014-01-02 16:36:57 -05:00
|
|
|
mStickyList.setOnItemClickListener(this);
|
|
|
|
mStickyList.setAreHeadersSticky(true);
|
|
|
|
mStickyList.setDrawingListUnderStickyHeader(false);
|
|
|
|
mStickyList.setFastScrollEnabled(true);
|
2014-01-02 15:10:08 -05:00
|
|
|
try {
|
2014-01-02 16:36:57 -05:00
|
|
|
mStickyList.setFastScrollAlwaysVisible(true);
|
2014-01-02 15:10:08 -05:00
|
|
|
} catch (ApiLevelTooLowException e) {
|
|
|
|
}
|
2012-10-30 19:39:32 -04:00
|
|
|
|
2014-01-03 12:07:42 -05:00
|
|
|
// this view is made visible if no data is available
|
|
|
|
mStickyList.setEmptyView(getActivity().findViewById(R.id.empty));
|
2014-01-01 10:54:55 -05:00
|
|
|
|
2014-01-05 18:58:04 -05:00
|
|
|
/*
|
|
|
|
* ActionBarSherlock does not support MultiChoiceModeListener. Thus multi-selection is only
|
|
|
|
* available for Android >= 3.0
|
|
|
|
*/
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
|
|
|
mStickyList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
|
|
|
|
mStickyList.getWrappedList().setMultiChoiceModeListener(new MultiChoiceModeListener() {
|
|
|
|
|
|
|
|
private int count = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
android.view.MenuInflater inflater = getActivity().getMenuInflater();
|
2014-01-07 16:59:39 -05:00
|
|
|
inflater.inflate(R.menu.key_list_public_multi, menu);
|
2014-01-05 18:58:04 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
|
|
|
Set<Integer> positions = mAdapter.getCurrentCheckedPosition();
|
2014-01-07 16:59:39 -05:00
|
|
|
|
|
|
|
// get IDs for checked positions as long array
|
|
|
|
long[] ids = new long[positions.size()];
|
|
|
|
int i = 0;
|
|
|
|
for (int pos : positions) {
|
|
|
|
ids[i] = mAdapter.getItemId(pos);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2014-01-05 18:58:04 -05:00
|
|
|
switch (item.getItemId()) {
|
2014-02-04 09:01:50 -05:00
|
|
|
case R.id.menu_key_list_public_multi_encrypt: {
|
|
|
|
encrypt(ids);
|
2014-01-07 16:59:39 -05:00
|
|
|
|
2014-02-04 09:01:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R.id.menu_key_list_public_multi_delete: {
|
|
|
|
showDeleteKeyDialog(ids);
|
2014-01-05 18:58:04 -05:00
|
|
|
|
2014-02-04 09:01:50 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-01-07 16:59:39 -05:00
|
|
|
}
|
2014-01-05 18:58:04 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
count = 0;
|
|
|
|
mAdapter.clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
|
2014-02-04 09:01:50 -05:00
|
|
|
boolean checked) {
|
2014-01-05 18:58:04 -05:00
|
|
|
if (checked) {
|
|
|
|
count++;
|
|
|
|
mAdapter.setNewSelection(position, checked);
|
|
|
|
} else {
|
|
|
|
count--;
|
|
|
|
mAdapter.removeSelection(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
String keysSelected = getResources().getQuantityString(
|
|
|
|
R.plurals.key_list_selected_keys, count, count);
|
|
|
|
mode.setTitle(keysSelected);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-03 12:07:42 -05:00
|
|
|
// NOTE: Not supported by StickyListHeader, thus no indicator is shown while loading
|
2014-01-01 16:26:19 -05:00
|
|
|
// Start out with a progress indicator.
|
2014-01-02 15:10:08 -05:00
|
|
|
// setListShown(false);
|
2013-01-08 16:12:23 -05:00
|
|
|
|
2014-01-01 16:26:19 -05:00
|
|
|
// Create an empty adapter we will use to display the loaded data.
|
2014-01-07 16:59:39 -05:00
|
|
|
mAdapter = new KeyListPublicAdapter(getActivity(), null, Id.type.public_key, USER_ID_INDEX);
|
2014-01-03 12:07:42 -05:00
|
|
|
mStickyList.setAdapter(mAdapter);
|
2012-10-30 19:39:32 -04:00
|
|
|
|
2014-01-01 16:26:19 -05:00
|
|
|
// Prepare the loader. Either re-connect with an existing one,
|
|
|
|
// or start a new one.
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
2012-10-30 19:39:32 -04:00
|
|
|
}
|
|
|
|
|
2012-10-28 18:51:03 -04:00
|
|
|
// These are the rows that we will retrieve.
|
2014-02-04 09:01:50 -05:00
|
|
|
static final String[] PROJECTION = new String[]{
|
|
|
|
KeychainContract.KeyRings._ID,
|
|
|
|
KeychainContract.KeyRings.MASTER_KEY_ID,
|
|
|
|
KeychainContract.UserIds.USER_ID,
|
|
|
|
KeychainContract.Keys.IS_REVOKED
|
|
|
|
};
|
2012-10-28 18:51:03 -04:00
|
|
|
|
2014-01-03 12:07:42 -05:00
|
|
|
static final int USER_ID_INDEX = 2;
|
|
|
|
|
2012-10-29 19:15:08 -04:00
|
|
|
static final String SORT_ORDER = UserIds.USER_ID + " ASC";
|
2012-10-28 18:51:03 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
// This is called when a new Loader needs to be created. This
|
|
|
|
// sample only has one Loader, so we don't care about the ID.
|
2012-10-29 19:15:08 -04:00
|
|
|
Uri baseUri = KeyRings.buildPublicKeyRingsUri();
|
2012-10-28 18:51:03 -04:00
|
|
|
|
|
|
|
// 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, SORT_ORDER);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
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.)
|
2014-01-03 12:07:42 -05:00
|
|
|
mAdapter.swapCursor(data);
|
2014-01-02 15:10:08 -05:00
|
|
|
|
2014-01-02 16:36:57 -05:00
|
|
|
mStickyList.setAdapter(mAdapter);
|
2012-10-28 18:51:03 -04:00
|
|
|
|
2014-01-03 12:07:42 -05:00
|
|
|
// NOTE: Not supported by StickyListHeader, thus no indicator is shown while loading
|
2012-10-28 18:51:03 -04:00
|
|
|
// The list should now be shown.
|
2014-01-03 12:07:42 -05:00
|
|
|
// if (isResumed()) {
|
|
|
|
// setListShown(true);
|
|
|
|
// } else {
|
|
|
|
// setListShownNoAnimation(true);
|
|
|
|
// }
|
2012-10-28 18:51:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
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.
|
2014-01-01 16:26:19 -05:00
|
|
|
mAdapter.swapCursor(null);
|
2012-10-28 18:51:03 -04:00
|
|
|
}
|
|
|
|
|
2014-01-02 16:36:57 -05:00
|
|
|
/**
|
|
|
|
* On click on item, start key view activity
|
|
|
|
*/
|
2014-01-02 15:10:08 -05:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
|
2014-01-19 11:54:26 -05:00
|
|
|
Intent viewIntent = null;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
|
|
|
|
viewIntent = new Intent(getActivity(), ViewKeyActivity.class);
|
|
|
|
} else {
|
|
|
|
viewIntent = new Intent(getActivity(), ViewKeyActivityJB.class);
|
|
|
|
}
|
|
|
|
viewIntent.setData(KeychainContract.KeyRings.buildPublicKeyRingsUri(Long.toString(id)));
|
|
|
|
startActivity(viewIntent);
|
2014-01-02 15:10:08 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 16:59:39 -05:00
|
|
|
public void encrypt(long[] keyRingRowIds) {
|
|
|
|
// get master key ids from row ids
|
|
|
|
long[] keyRingIds = new long[keyRingRowIds.length];
|
|
|
|
for (int i = 0; i < keyRingRowIds.length; i++) {
|
|
|
|
keyRingIds[i] = ProviderHelper.getPublicMasterKeyId(getActivity(), keyRingRowIds[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = new Intent(getActivity(), EncryptActivity.class);
|
|
|
|
intent.setAction(EncryptActivity.ACTION_ENCRYPT);
|
|
|
|
intent.putExtra(EncryptActivity.EXTRA_ENCRYPTION_KEY_IDS, keyRingIds);
|
|
|
|
// used instead of startActivity set actionbar based on callingPackage
|
|
|
|
startActivityForResult(intent, 0);
|
|
|
|
}
|
|
|
|
|
2014-01-05 18:58:04 -05:00
|
|
|
/**
|
|
|
|
* Show dialog to delete key
|
2014-02-04 09:01:50 -05:00
|
|
|
*
|
2014-01-05 18:58:04 -05:00
|
|
|
* @param keyRingRowIds
|
|
|
|
*/
|
|
|
|
public void showDeleteKeyDialog(long[] keyRingRowIds) {
|
2014-01-07 15:02:31 -05:00
|
|
|
DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(null,
|
2014-01-05 18:58:04 -05:00
|
|
|
keyRingRowIds, Id.type.public_key);
|
|
|
|
|
|
|
|
deleteKeyDialog.show(getActivity().getSupportFragmentManager(), "deleteKeyDialog");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|