|
|
|
|
@@ -18,39 +18,45 @@
|
|
|
|
|
package org.sufficientlysecure.keychain.remote.ui;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.arch.lifecycle.LiveData;
|
|
|
|
|
import android.arch.lifecycle.ViewModelProviders;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.support.v4.app.LoaderManager;
|
|
|
|
|
import android.support.v4.content.CursorLoader;
|
|
|
|
|
import android.support.v4.content.Loader;
|
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
|
|
|
|
|
import org.openintents.openpgp.util.OpenPgpApi;
|
|
|
|
|
import org.openintents.openpgp.util.OpenPgpUtils;
|
|
|
|
|
import org.sufficientlysecure.keychain.R;
|
|
|
|
|
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
|
|
|
|
import org.sufficientlysecure.keychain.pgp.KeyRing;
|
|
|
|
|
import org.sufficientlysecure.keychain.provider.ApiAppDao;
|
|
|
|
|
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
|
|
|
|
|
import org.sufficientlysecure.keychain.remote.ui.adapter.SelectSignKeyAdapter;
|
|
|
|
|
import org.sufficientlysecure.keychain.provider.KeyRepository;
|
|
|
|
|
import org.sufficientlysecure.keychain.ui.CreateKeyActivity;
|
|
|
|
|
import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter;
|
|
|
|
|
import org.sufficientlysecure.keychain.ui.base.RecyclerFragment;
|
|
|
|
|
import org.sufficientlysecure.keychain.ui.util.adapter.CursorAdapter;
|
|
|
|
|
import org.sufficientlysecure.keychain.ui.keyview.GenericViewModel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SelectSignKeyIdListFragment extends RecyclerFragment<SelectSignKeyAdapter>
|
|
|
|
|
implements SelectSignKeyAdapter.SelectSignKeyListener, LoaderManager.LoaderCallbacks<Cursor> {
|
|
|
|
|
|
|
|
|
|
public class SelectSignKeyIdListFragment extends RecyclerFragment<KeyChoiceAdapter> {
|
|
|
|
|
private static final String ARG_PACKAGE_NAME = "package_name";
|
|
|
|
|
private static final String ARG_PREF_UID = "pref_uid";
|
|
|
|
|
public static final String ARG_DATA = "data";
|
|
|
|
|
|
|
|
|
|
private Intent mResult;
|
|
|
|
|
private String mPrefUid;
|
|
|
|
|
private ApiAppDao mApiAppDao;
|
|
|
|
|
private String mPackageName;
|
|
|
|
|
private ApiAppDao apiAppDao;
|
|
|
|
|
private KeyRepository keyRepository;
|
|
|
|
|
|
|
|
|
|
private KeyChoiceAdapter keyChoiceAdapter;
|
|
|
|
|
|
|
|
|
|
private Intent resultIntent;
|
|
|
|
|
private String prefUid;
|
|
|
|
|
private String packageName;
|
|
|
|
|
|
|
|
|
|
public static SelectSignKeyIdListFragment newInstance(String packageName, Intent data, String preferredUserId) {
|
|
|
|
|
SelectSignKeyIdListFragment frag = new SelectSignKeyIdListFragment();
|
|
|
|
|
@@ -68,112 +74,74 @@ public class SelectSignKeyIdListFragment extends RecyclerFragment<SelectSignKeyA
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
mApiAppDao = ApiAppDao.getInstance(getActivity());
|
|
|
|
|
apiAppDao = ApiAppDao.getInstance(requireContext());
|
|
|
|
|
keyRepository = KeyRepository.create(requireContext());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
|
|
|
|
|
LinearLayout linearLayout = new LinearLayout(requireContext());
|
|
|
|
|
linearLayout.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
|
|
|
|
|
|
View dummyItemView = inflater.inflate(R.layout.key_list_dummy, linearLayout, false);
|
|
|
|
|
linearLayout.addView(dummyItemView, new LinearLayout.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
|
|
|
|
|
|
|
|
View recyclerView = super.onCreateView(inflater, parent, savedInstanceState);
|
|
|
|
|
linearLayout.addView(recyclerView, new LinearLayout.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
|
|
|
|
|
|
|
|
|
dummyItemView.setOnClickListener((v) -> onCreateKeyDummyClicked());
|
|
|
|
|
|
|
|
|
|
return linearLayout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Define Adapter and Loader on create of Activity
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
mResult = getArguments().getParcelable(ARG_DATA);
|
|
|
|
|
mPrefUid = getArguments().getString(ARG_PREF_UID);
|
|
|
|
|
mPackageName = getArguments().getString(ARG_PACKAGE_NAME);
|
|
|
|
|
resultIntent = getArguments().getParcelable(ARG_DATA);
|
|
|
|
|
prefUid = getArguments().getString(ARG_PREF_UID);
|
|
|
|
|
packageName = getArguments().getString(ARG_PACKAGE_NAME);
|
|
|
|
|
|
|
|
|
|
// Give some text to display if there is no data. In a real
|
|
|
|
|
// application this would come from a resource.
|
|
|
|
|
setEmptyText(getString(R.string.list_empty));
|
|
|
|
|
|
|
|
|
|
SelectSignKeyAdapter adapter = new SelectSignKeyAdapter(getContext(), null);
|
|
|
|
|
adapter.setListener(this);
|
|
|
|
|
|
|
|
|
|
setAdapter(adapter);
|
|
|
|
|
setLayoutManager(new LinearLayoutManager(getContext()));
|
|
|
|
|
|
|
|
|
|
// Start out with a progress indicator.
|
|
|
|
|
hideList(false);
|
|
|
|
|
|
|
|
|
|
// Prepare the loader. Either re-connect with an existing one,
|
|
|
|
|
// or start a new one.
|
|
|
|
|
getLoaderManager().initLoader(0, null, this);
|
|
|
|
|
GenericViewModel viewModel = ViewModelProviders.of(this).get(GenericViewModel.class);
|
|
|
|
|
LiveData<List<UnifiedKeyInfo>> liveData = viewModel.getGenericLiveData(
|
|
|
|
|
requireContext(), keyRepository::getAllUnifiedKeyInfoWithSecret);
|
|
|
|
|
liveData.observe(this, this::onLoadUnifiedKeyData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
|
|
|
Uri baseUri = KeyRings.buildUnifiedKeyRingsUri();
|
|
|
|
|
|
|
|
|
|
// These are the rows that we will retrieve.
|
|
|
|
|
String[] projection = new String[]{
|
|
|
|
|
KeyRings._ID,
|
|
|
|
|
KeyRings.MASTER_KEY_ID,
|
|
|
|
|
KeyRings.USER_ID,
|
|
|
|
|
KeyRings.IS_EXPIRED,
|
|
|
|
|
KeyRings.IS_REVOKED,
|
|
|
|
|
KeyRings.HAS_ENCRYPT,
|
|
|
|
|
KeyRings.VERIFIED,
|
|
|
|
|
KeyRings.HAS_ANY_SECRET,
|
|
|
|
|
KeyRings.HAS_DUPLICATE_USER_ID,
|
|
|
|
|
KeyRings.CREATION,
|
|
|
|
|
KeyRings.NAME,
|
|
|
|
|
KeyRings.EMAIL,
|
|
|
|
|
KeyRings.COMMENT
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
String selection = KeyRings.HAS_ANY_SECRET + " != 0";
|
|
|
|
|
|
|
|
|
|
String orderBy = KeyRings.USER_ID + " ASC";
|
|
|
|
|
// 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, selection, null, orderBy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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.)
|
|
|
|
|
getAdapter().swapCursor(CursorAdapter.KeyCursor.wrap(data));
|
|
|
|
|
|
|
|
|
|
// The list should now be shown.
|
|
|
|
|
if (isResumed()) {
|
|
|
|
|
showList(true);
|
|
|
|
|
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
|
|
|
|
if (keyChoiceAdapter == null) {
|
|
|
|
|
keyChoiceAdapter = KeyChoiceAdapter.createSingleClickableAdapter(data, this::onSelectKeyItemClicked);
|
|
|
|
|
setAdapter(keyChoiceAdapter);
|
|
|
|
|
} else {
|
|
|
|
|
showList(false);
|
|
|
|
|
keyChoiceAdapter.setUnifiedKeyInfoItems(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean animateShowList = !isResumed();
|
|
|
|
|
showList(animateShowList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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.
|
|
|
|
|
getAdapter().swapCursor(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
getAdapter().setListener(null);
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreateKeyDummyClicked() {
|
|
|
|
|
OpenPgpUtils.UserId userIdSplit = KeyRing.splitUserId(mPrefUid);
|
|
|
|
|
OpenPgpUtils.UserId userIdSplit = KeyRing.splitUserId(prefUid);
|
|
|
|
|
|
|
|
|
|
Intent intent = new Intent(getActivity(), CreateKeyActivity.class);
|
|
|
|
|
intent.putExtra(CreateKeyActivity.EXTRA_NAME, userIdSplit.name);
|
|
|
|
|
intent.putExtra(CreateKeyActivity.EXTRA_EMAIL, userIdSplit.email);
|
|
|
|
|
getActivity().startActivityForResult(intent, SelectSignKeyIdActivity.REQUEST_CODE_CREATE_KEY);
|
|
|
|
|
|
|
|
|
|
requireActivity().startActivityForResult(intent, SelectSignKeyIdActivity.REQUEST_CODE_CREATE_KEY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSelectKeyItemClicked(long masterKeyId) {
|
|
|
|
|
mApiAppDao.addAllowedKeyIdForApp(mPackageName, masterKeyId);
|
|
|
|
|
mResult.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, masterKeyId);
|
|
|
|
|
private void onSelectKeyItemClicked(UnifiedKeyInfo keyInfo) {
|
|
|
|
|
apiAppDao.addAllowedKeyIdForApp(packageName, keyInfo.master_key_id());
|
|
|
|
|
resultIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, keyInfo.master_key_id());
|
|
|
|
|
|
|
|
|
|
getActivity().setResult(Activity.RESULT_OK, mResult);
|
|
|
|
|
getActivity().finish();
|
|
|
|
|
Activity activity = requireActivity();
|
|
|
|
|
activity.setResult(Activity.RESULT_OK, resultIntent);
|
|
|
|
|
activity.finish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|