Share with NFC, Qr Code sharing refactored, Receiving NFC is not ready yet

This commit is contained in:
Dominik Schürmann
2013-01-08 12:58:21 +01:00
parent 9758cb673f
commit 6cc7156fbd
18 changed files with 459 additions and 59 deletions

View File

@@ -25,6 +25,9 @@ public final class Constants {
public static final String TAG = "APG";
public static final String PACKAGE_NAME = "org.thialfihar.android.apg";
public static final String NFC_MIME = "application/org.thialfihar.android.apg";
public static final String PERMISSION_ACCESS_KEY_DATABASE = PACKAGE_NAME
+ ".permission.ACCESS_KEY_DATABASE";

View File

@@ -36,7 +36,8 @@ public final class Id {
public static final int update = 0x21070004;
public static final int exportToServer = 0x21070005;
public static final int share_qr_code = 0x21070006;
public static final int signKey = 0x21070007;
public static final int share_nfc = 0x21070007;
public static final int signKey = 0x21070008;
public static final class option {
public static final int new_pass_phrase = 0x21070001;

View File

@@ -16,7 +16,6 @@
package org.thialfihar.android.apg.helper;
import java.io.InputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
@@ -30,27 +29,12 @@ import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
public class OtherHelper {
/**
* Gets input stream of raw resource
*
* @param context
* current context
* @param resourceID
* of html file to read
* @return input stream of resource
*/
public static InputStream getInputStreamFromResource(Context context, int resourceID) {
InputStream raw = context.getResources().openRawResource(resourceID);
return raw;
}
/**
* Return the number if days between two dates
*

View File

@@ -37,6 +37,8 @@ import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class HelpActivity extends SherlockFragmentActivity {
public static final String EXTRA_SELECTED_TAB = "selectedTab";
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
@@ -74,18 +76,29 @@ public class HelpActivity extends SherlockFragmentActivity {
mTabsAdapter = new TabsAdapter(this, mViewPager);
int selectedTab = 0;
Intent intent = getIntent();
if (intent.getExtras() != null && intent.getExtras().containsKey(EXTRA_SELECTED_TAB)) {
selectedTab = intent.getExtras().getInt(EXTRA_SELECTED_TAB);
}
Bundle startBundle = new Bundle();
startBundle.putInt(HelpFragmentHtml.ARG_HTML_FILE, R.raw.help_start);
mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_start)),
HelpFragmentHtml.class, startBundle);
HelpFragmentHtml.class, startBundle, (selectedTab == 0 ? true : false));
Bundle nfcBundle = new Bundle();
nfcBundle.putInt(HelpFragmentHtml.ARG_HTML_FILE, R.raw.help_nfc_beam);
mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_nfc_beam)),
HelpFragmentHtml.class, nfcBundle, (selectedTab == 1 ? true : false));
Bundle changelogBundle = new Bundle();
changelogBundle.putInt(HelpFragmentHtml.ARG_HTML_FILE, R.raw.help_changelog);
mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_changelog)),
HelpFragmentHtml.class, changelogBundle);
HelpFragmentHtml.class, changelogBundle, (selectedTab == 2 ? true : false));
mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.help_tab_about)),
HelpFragmentAbout.class, null);
HelpFragmentAbout.class, null, (selectedTab == 3 ? true : false));
}
public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener,
@@ -114,12 +127,12 @@ public class HelpActivity extends SherlockFragmentActivity {
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args, boolean selected) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
mActionBar.addTab(tab, selected);
notifyDataSetChanged();
}

View File

@@ -57,8 +57,7 @@ public class HelpFragmentAbout extends SherlockFragment {
View view = inflater.inflate(R.layout.help_fragment_about, container, false);
// load html from html file from /res/raw
InputStream inputStreamText = OtherHelper.getInputStreamFromResource(this.getActivity(),
R.raw.help_about);
InputStream inputStreamText = getResources().openRawResource(R.raw.help_about);
TextView versionText = (TextView) view.findViewById(R.id.help_about_version);
versionText.setText(getString(R.string.help_about_version) + " " + getVersion());

View File

@@ -23,6 +23,7 @@ import net.nightwhistler.htmlspanner.HtmlSpanner;
import net.nightwhistler.htmlspanner.JellyBeanSpanFixTextView;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.OtherHelper;
import org.thialfihar.android.apg.util.Log;
@@ -74,8 +75,7 @@ public class HelpFragmentHtml extends SherlockFragment {
htmlFile = getArguments().getInt(ARG_HTML_FILE);
// load html from html file from /res/raw
InputStream inputStreamText = OtherHelper.getInputStreamFromResource(this.getActivity(),
htmlFile);
InputStream inputStreamText = getResources().openRawResource(htmlFile);
mActivity = getActivity();

View File

@@ -49,15 +49,14 @@ import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ImportKeysActivity extends SherlockFragmentActivity {
public static final String ACTION = Constants.INTENT_PREFIX + "IMPORT";
public static final String ACTION_IMPORT = Constants.INTENT_PREFIX + "IMPORT";
public static final String ACTION_IMPORT_FROM_FILE = Constants.INTENT_PREFIX
+ "IMPORT_FROM_FILE";
public static final String ACTION_IMPORT_FROM_QR_CODE = Constants.INTENT_PREFIX
+ "IMPORT_FROM_QR_CODE";
public static final String ACTION_IMPORT_FROM_NFC = Constants.INTENT_PREFIX + "IMPORT_FROM_NFC";
// only used by IMPORT_AND_RETURN
// only used by IMPORT
public static final String EXTRA_TEXT = "text";
protected String mImportFilename = Constants.path.APP_DIR + "/";
@@ -93,11 +92,14 @@ public class ImportKeysActivity extends SherlockFragmentActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, Id.menu.option.import_from_file, 0, R.string.menu_importFromFile)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.import_from_qr_code, 1, R.string.menu_importFromQrCode)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.import_from_nfc, 2, R.string.menu_importFromNfc)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@@ -115,16 +117,14 @@ public class ImportKeysActivity extends SherlockFragmentActivity {
case Id.menu.option.import_from_file:
showImportKeysDialog();
return true;
case Id.menu.option.import_from_qr_code:
importFromQrCode();
return true;
case Id.menu.option.import_from_nfc:
importFromNfc();
return true;
default:
@@ -147,13 +147,13 @@ public class ImportKeysActivity extends SherlockFragmentActivity {
if (Intent.ACTION_VIEW.equals(action)) {
// Android's Action when opening file associated to APG (see AndroidManifest.xml)
// override action to delegate it to APGs ACTION_IMPORT
action = ACTION;
action = ACTION_IMPORT;
}
/**
* APG's own Actions
*/
if (ACTION.equals(action)) {
if (ACTION_IMPORT.equals(action)) {
if ("file".equals(intent.getScheme()) && intent.getDataString() != null) {
mImportFilename = intent.getData().getPath();
} else {
@@ -168,7 +168,7 @@ public class ImportKeysActivity extends SherlockFragmentActivity {
} else if (ACTION_IMPORT_FROM_QR_CODE.equals(action)) {
importFromQrCode();
} else if (ACTION_IMPORT_FROM_NFC.equals(action)) {
importFromNfc();
}
}
@@ -176,6 +176,13 @@ public class ImportKeysActivity extends SherlockFragmentActivity {
new IntentIntegrator(this).initiateScan();
}
private void importFromNfc() {
// show nfc help
Intent intent = new Intent(this, HelpActivity.class);
intent.putExtra(HelpActivity.EXTRA_SELECTED_TAB, 1);
startActivityForResult(intent, 0);
}
/**
* Show to dialog from where to import keys
*/

View File

@@ -17,8 +17,6 @@
package org.thialfihar.android.apg.ui;
import java.util.ArrayList;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R;
@@ -28,8 +26,6 @@ import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ApgContract.UserIds;
import org.thialfihar.android.apg.ui.widget.KeyListAdapter;
import com.google.zxing.integration.android.IntentIntegrator;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
@@ -41,7 +37,6 @@ import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class KeyListPublicFragment extends KeyListFragment implements
@@ -81,7 +76,8 @@ public class KeyListPublicFragment extends KeyListFragment implements
menu.add(0, Id.menu.update, 1, R.string.menu_updateKey);
menu.add(0, Id.menu.signKey, 2, R.string.menu_signKey);
menu.add(0, Id.menu.exportToServer, 3, R.string.menu_exportKeyToServer);
menu.add(0, Id.menu.share_qr_code, 6, R.string.menu_share);
menu.add(0, Id.menu.share_qr_code, 6, R.string.menu_shareQrCode);
menu.add(0, Id.menu.share_nfc, 7, R.string.menu_shareNfc);
}
@@ -145,7 +141,22 @@ public class KeyListPublicFragment extends KeyListFragment implements
case Id.menu.share_qr_code:
// get master key id using row id
long masterKeyId = ProviderHelper.getPublicMasterKeyId(mKeyListActivity, keyRingRowId);
shareByQrCode(masterKeyId);
Intent qrCodeIntent = new Intent(mKeyListActivity, ShareQrCodeActivity.class);
qrCodeIntent.setAction(ShareQrCodeActivity.ACTION_SHARE_WITH_QR_CODE);
qrCodeIntent.putExtra(ShareQrCodeActivity.EXTRA_MASTER_KEY_ID, masterKeyId);
startActivityForResult(qrCodeIntent, 0);
return true;
case Id.menu.share_nfc:
// get master key id using row id
long masterKeyId2 = ProviderHelper.getPublicMasterKeyId(mKeyListActivity, keyRingRowId);
Intent nfcIntent = new Intent(mKeyListActivity, ShareNfcBeamActivity.class);
nfcIntent.setAction(ShareNfcBeamActivity.ACTION_SHARE_WITH_NFC);
nfcIntent.putExtra(ShareNfcBeamActivity.EXTRA_MASTER_KEY_ID, masterKeyId2);
startActivityForResult(nfcIntent, 0);
return true;
@@ -155,12 +166,6 @@ public class KeyListPublicFragment extends KeyListFragment implements
}
}
private void shareByQrCode(long masterKeyId) {
ArrayList<String> keyringArmored = ProviderHelper.getPublicKeyRingsAsArmoredString(
mKeyListActivity, new long[] { masterKeyId });
new IntentIntegrator(mKeyListActivity).shareText(keyringArmored.get(0));
}
// These are the rows that we will retrieve.
static final String[] PROJECTION = new String[] { KeyRings._ID, KeyRings.MASTER_KEY_ID,
UserIds.USER_ID };

View File

@@ -228,7 +228,7 @@ public class KeyServerQueryActivity extends SherlockFragmentActivity {
if (mKeyData != null) {
Intent intent = new Intent(KeyServerQueryActivity.this,
KeyListPublicActivity.class);
intent.setAction(ImportKeysActivity.ACTION);
intent.setAction(ImportKeysActivity.ACTION_IMPORT);
intent.putExtra(ImportKeysActivity.EXTRA_TEXT, mKeyData);
startActivity(intent);
}

View File

@@ -0,0 +1,237 @@
/*
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui;
import java.io.IOException;
import java.io.InputStream;
import net.nightwhistler.htmlspanner.HtmlSpanner;
import net.nightwhistler.htmlspanner.JellyBeanSpanFixTextView;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.OtherHelper;
import org.thialfihar.android.apg.provider.ProviderHelper;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import android.annotation.TargetApi;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.text.method.LinkMovementMethod;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class ShareNfcBeamActivity extends SherlockFragmentActivity implements
CreateNdefMessageCallback, OnNdefPushCompleteCallback {
public static final String ACTION_SHARE_WITH_NFC = Constants.INTENT_PREFIX + "SHARE_WITH_NFC";
public static final String EXTRA_MASTER_KEY_ID = "masterKeyId";
NfcAdapter mNfcAdapter;
byte[] mSharedKeyringBytes;
private static final int MESSAGE_SENT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
Toast.makeText(this,
getString(R.string.error) + ": " + getString(R.string.error_jellyBeanNeeded),
Toast.LENGTH_LONG).show();
finish();
} else {
// Check for available NFC Adapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(this,
getString(R.string.error) + ": " + getString(R.string.error_nfcNeeded),
Toast.LENGTH_LONG).show();
finish();
} else {
buildView();
// handle actions after verifying that nfc works...
handleActions(getIntent());
}
}
}
protected void handleActions(Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (extras == null) {
extras = new Bundle();
}
if (ACTION_SHARE_WITH_NFC.equals(action)) {
long masterKeyId = getIntent().getExtras().getLong(EXTRA_MASTER_KEY_ID);
// get public keyring as byte array
mSharedKeyringBytes = ProviderHelper.getPublicKeyRingsAsByteArray(this,
new long[] { masterKeyId });
// Register callback to set NDEF message
mNfcAdapter.setNdefPushMessageCallback(this, this);
// Register callback to listen for message-sent success
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
}
}
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
void handleActionNdefDiscovered(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
byte[] receivedKeyringBytes = msg.getRecords()[0].getPayload();
// Log.d(Constants.TAG, new String());
}
private void buildView() {
// load html from html file from /res/raw
InputStream inputStreamText = getResources().openRawResource(R.raw.nfc_beam_share);
setContentView(R.layout.share_nfc_beam);
JellyBeanSpanFixTextView text = (JellyBeanSpanFixTextView) findViewById(R.id.nfc_beam_text);
// load html into textview
HtmlSpanner htmlSpanner = new HtmlSpanner();
htmlSpanner.setStripExtraWhiteSpace(true);
try {
text.setText(htmlSpanner.fromHtml(inputStreamText));
} catch (IOException e) {
Log.e(Constants.TAG, "Error while reading raw resources as stream", e);
}
// make links work
text.setMovementMethod(LinkMovementMethod.getInstance());
// no flickering when clicking textview for Android < 4
text.setTextColor(getResources().getColor(android.R.color.black));
// set actionbar without home button if called from another app
OtherHelper.setActionBarBackButton(this);
}
/**
* Implementation for the CreateNdefMessageCallback interface
*/
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
/**
* When a device receives a push with an AAR in it, the application specified in the AAR is
* guaranteed to run. The AAR overrides the tag dispatch system. You can add it back in to
* guarantee that this activity starts when receiving a beamed message. For now, this code
* uses the tag dispatch system.
*/
NdefMessage msg = new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME,
mSharedKeyringBytes), NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME));
return msg;
}
/**
* Implementation for the OnNdefPushCompleteCallback interface
*/
@Override
public void onNdefPushComplete(NfcEvent arg0) {
// A handler is needed to send messages to the activity when this
// callback occurs, because it happens from a binder thread
mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
}
/** This handler receives a message from onNdefPushComplete */
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_SENT:
Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
public void onResume() {
super.onResume();
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
handleActionNdefDiscovered(getIntent());
}
}
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.nfc_beam, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in Action Bar clicked; go to KeyListPublicActivity
Intent intent = new Intent(this, KeyListPublicActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_settings:
Intent intentSettings = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
startActivity(intentSettings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thialfihar.android.apg.ui;
import java.util.ArrayList;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.provider.ProviderHelper;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import android.content.Intent;
import android.os.Bundle;
public class ShareQrCodeActivity extends SherlockFragmentActivity {
public static final String ACTION_SHARE_WITH_QR_CODE = Constants.INTENT_PREFIX
+ "SHARE_WITH_QR_CODE";
public static final String EXTRA_MASTER_KEY_ID = "masterKeyId";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleActions(getIntent());
}
protected void handleActions(Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (extras == null) {
extras = new Bundle();
}
if (ACTION_SHARE_WITH_QR_CODE.equals(action)) {
long masterKeyId = getIntent().getExtras().getLong(EXTRA_MASTER_KEY_ID);
// get public keyring as ascii armored string
ArrayList<String> keyringArmored = ProviderHelper.getPublicKeyRingsAsArmoredString(
this, new long[] { masterKeyId });
// close this activity
finish();
// use barcode scanner integration library
new IntentIntegrator(this).shareText(keyringArmored.get(0));
}
}
}