intent demo, fix some intents

This commit is contained in:
Dominik Schürmann 2014-02-15 20:14:23 +01:00
parent e70886e242
commit 81b810e9f6
8 changed files with 345 additions and 17 deletions

View File

@ -25,6 +25,9 @@
android:name=".OpenPgpProviderActivity"
android:label="OpenPGP Provider"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".IntentActivity"
android:label="Intents" />
</application>
</manifest>

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2012-2014 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.
@ -44,8 +44,7 @@ public class BaseActivity extends PreferenceActivity {
mIntentDemo.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// startActivity(new Intent(mActivity, IntentDemoActivity.class));
Toast.makeText(BaseActivity.this, "Not implemented!", Toast.LENGTH_LONG).show();
startActivity(new Intent(BaseActivity.this, IntentActivity.class));
return false;
}

View File

@ -0,0 +1,209 @@
/*
* Copyright (C) 2014 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.sufficientlysecure.keychain.demo;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import org.sufficientlysecure.keychain.api.KeychainIntents;
public class IntentActivity extends PreferenceActivity {
private static final int SELECT_PHOTO = 100;
/**
* Called when the activity is first created.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load preferences from xml
addPreferencesFromResource(R.xml.intent_preference);
// find preferences
Preference encrypt = (Preference) findPreference("ENCRYPT");
Preference encryptUri = (Preference) findPreference("ENCRYPT_URI");
Preference decrypt = (Preference) findPreference("DECRYPT");
Preference import_key = (Preference) findPreference("IMPORT_KEY");
Preference import_key_from_keyserver = (Preference) findPreference("IMPORT_KEY_FROM_KEYSERVER");
Preference import_key_from_qr_code = (Preference) findPreference("IMPORT_KEY_FROM_QR_CODE");
Preference openpgp4fpr = (Preference) findPreference("openpgp4fpr");
encrypt.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
try {
Intent intent = new Intent(KeychainIntents.ENCRYPT);
intent.putExtra(KeychainIntents.ENCRYPT_EXTRA_TEXT, "Hello world!");
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentActivity.this, "Activity not found!", Toast.LENGTH_LONG).show();
}
return false;
}
});
encryptUri.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
return false;
}
});
decrypt.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
String text = "-----BEGIN PGP SIGNED MESSAGE-----\n" +
"Hash: SHA1\n" +
"\n" +
"Hello world!\n" +
"-----BEGIN PGP SIGNATURE-----\n" +
"Version: GnuPG v1.4.12 (GNU/Linux)\n" +
"Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/\n" +
"\n" +
"iQEcBAEBAgAGBQJS/7vTAAoJEHGMBwEAASKCkGYH/2jBLzamVyqd61jrjMQM0jUv\n" +
"MkDcPUxPrYH3wZOO0HcgdBQEo66GZEC2ATmo8izJUMk35Q5jas99k0ac9pXhPUPE\n" +
"5qDXdQS10S07R6J0SeDYFWDSyrSiDTCZpFkVu3JGP/3S0SkMYXPzfYlh8Ciuxu7i\n" +
"FR5dmIiz3VQaBgTBSCBFEomNFM5ypynBJqKIzIty8v0NbV72Rtg6Xg76YqWQ/6MC\n" +
"/MlT3y3++HhfpEmLf5WLEXljbuZ4SfCybgYXG9gBzhJu3+gmBoSicdYTZDHSxBBR\n" +
"BwI+ueLbhgRz+gU+WJFE7xNw35xKtBp1C4PR0iKI8rZCSHLjsRVzor7iwDaR51M=\n" +
"=3Ydc\n" +
"-----END PGP SIGNATURE-----";
try {
Intent intent = new Intent(KeychainIntents.DECRYPT);
intent.putExtra(KeychainIntents.DECRYPT_EXTRA_TEXT, text);
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentActivity.this, "Activity not found!", Toast.LENGTH_LONG).show();
}
return false;
}
});
import_key.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
try {
Intent intent = new Intent(KeychainIntents.IMPORT_KEY);
// intent.putExtra(KeychainIntents.IMPORT_KEY_EXTRA_KEY_BYTES, TODO);
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentActivity.this, "Activity not found!", Toast.LENGTH_LONG).show();
}
return false;
}
});
import_key_from_keyserver.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
try {
Intent intent = new Intent(KeychainIntents.IMPORT_KEY_FROM_KEYSERVER);
intent.putExtra(KeychainIntents.IMPORT_KEY_FROM_KEYSERVER_QUERY, "Richard Stallman");
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentActivity.this, "Activity not found!", Toast.LENGTH_LONG).show();
}
return false;
}
});
import_key_from_qr_code.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
try {
Intent intent = new Intent(KeychainIntents.IMPORT_KEY_FROM_QR_CODE);
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentActivity.this, "Activity not found!", Toast.LENGTH_LONG).show();
}
return false;
}
});
openpgp4fpr.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("openpgp4fpr:73EE2314F65FA92EC2390D3A718C070100012282"));
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentActivity.this, "Activity not found!", Toast.LENGTH_LONG).show();
}
return false;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
// TODO: after fixing DECRYPT, we could use Uri selectedImage directly
Log.d(Constants.TAG, "filePath: " + filePath);
try {
Intent intent = new Intent(KeychainIntents.ENCRYPT);
Uri dataUri = Uri.parse("file://" + filePath);
Log.d(Constants.TAG, "Uri: " + dataUri);
intent.setData(dataUri);
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentActivity.this, "Activity not found!", Toast.LENGTH_LONG).show();
}
}
}
}
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Intents (org.sufficientlysecure.keychain.action.)">
<Preference
android:key="ENCRYPT"
android:title="ENCRYPT" />
<Preference
android:key="ENCRYPT_URI"
android:title="ENCRYPT with Uri" />
<Preference
android:key="DECRYPT"
android:title="DECRYPT" />
<Preference
android:key="IMPORT_KEY"
android:title="IMPORT_KEY" />
<Preference
android:key="IMPORT_KEY_FROM_KEYSERVER"
android:title="IMPORT_KEY_FROM_KEYSERVER" />
<Preference
android:key="IMPORT_KEY_FROM_QR_CODE"
android:title="IMPORT_KEY_FROM_QR_CODE" />
</PreferenceCategory>
<PreferenceCategory android:title="Special Intents">
<org.openintents.openpgp.util.OpenPgpListPreference
android:key="openpgp4fpr"
android:title="VIEW openpgp4fpr:73EE2314F65FA92EC2390D3A718C070100012282" />
</PreferenceCategory>
</PreferenceScreen>

View File

@ -23,7 +23,6 @@ public class OpenPgpConstants {
public static final int API_VERSION = 1;
public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService";
/* Bundle params */
public static final String PARAMS_API_VERSION = "api_version";
// request ASCII Armor for output

View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2014 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.sufficientlysecure.keychain.api;
public class KeychainIntents {
public static final String ENCRYPT = "org.sufficientlysecure.keychain.action.ENCRYPT";
public static final String ENCRYPT_EXTRA_TEXT = "text";
public static final String ENCRYPT_ASCII_ARMOR = "ascii_armor";
public static final String DECRYPT = "org.sufficientlysecure.keychain.action.DECRYPT";
public static final String DECRYPT_EXTRA_TEXT = "text";
public static final String IMPORT_KEY = "org.sufficientlysecure.keychain.action.IMPORT_KEY";
public static final String IMPORT_KEY_EXTRA_KEY_BYTES = "key_bytes";
public static final String IMPORT_KEY_FROM_KEYSERVER = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_KEYSERVER";
public static final String IMPORT_KEY_FROM_KEYSERVER_QUERY = "query";
public static final String IMPORT_KEY_FROM_KEYSERVER_FINGERPRINT = "fingerprint";
public static final String IMPORT_KEY_FROM_QR_CODE = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_QR_CODE";
}

View File

@ -153,12 +153,19 @@
android:windowSoftInputMode="stateHidden">
<!-- Keychain's own Actions -->
<!-- ENCRYPT with text as extra -->
<intent-filter>
<action android:name="org.sufficientlysecure.keychain.action.ENCRYPT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- ENCRYPT with data Uri -->
<intent-filter>
<action android:name="org.sufficientlysecure.keychain.action.ENCRYPT" />
<data android:mimeType="*/*" />
<category android:name="android.intent.category.DEFAULT" />
<!-- TODO: accept other schemes! -->
<data android:scheme="file" />
</intent-filter>
<!-- Android's Send Action -->
<intent-filter android:label="@string/intent_send_encrypt">
@ -176,12 +183,19 @@
android:windowSoftInputMode="stateHidden">
<!-- Keychain's own Actions -->
<!-- DECRYPT with text as extra -->
<intent-filter>
<action android:name="org.sufficientlysecure.keychain.action.DECRYPT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- DECRYPT with data Uri -->
<intent-filter>
<action android:name="org.sufficientlysecure.keychain.action.DECRYPT" />
<data android:mimeType="*/*" />
<category android:name="android.intent.category.DEFAULT" />
<!-- TODO: accept other schemes! -->
<data android:scheme="file" />
</intent-filter>
<!-- Android's Send Action -->
<intent-filter android:label="@string/intent_send_decrypt">

View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2014 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.sufficientlysecure.keychain.api;
public class KeychainIntents {
public static final String ENCRYPT = "org.sufficientlysecure.keychain.action.ENCRYPT";
public static final String ENCRYPT_EXTRA_TEXT = "text";
public static final String ENCRYPT_ASCII_ARMOR = "ascii_armor";
public static final String DECRYPT = "org.sufficientlysecure.keychain.action.DECRYPT";
public static final String DECRYPT_EXTRA_TEXT = "text";
public static final String IMPORT_KEY = "org.sufficientlysecure.keychain.action.IMPORT_KEY";
public static final String IMPORT_KEY_EXTRA_KEY_BYTES = "key_bytes";
public static final String IMPORT_KEY_FROM_KEYSERVER = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_KEYSERVER";
public static final String IMPORT_KEY_FROM_KEYSERVER_QUERY = "query";
public static final String IMPORT_KEY_FROM_KEYSERVER_FINGERPRINT = "fingerprint";
public static final String IMPORT_KEY_FROM_QR_CODE = "org.sufficientlysecure.keychain.action.IMPORT_KEY_FROM_QR_CODE";
}