aboutsummaryrefslogtreecommitdiffstats
path: root/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/IntentActivity.java
blob: 8fe0d2d9c29abbe415ebc62576455de52e209ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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();
                    }
                }
        }
    }

}