aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java
blob: a2e781e8a4e2273b8d246a292a1e50d819e75eba (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (C) 2013-2014 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.remote.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.BulletSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;

import org.openintents.openpgp.util.OpenPgpApi;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.remote.AccountSettings;
import org.sufficientlysecure.keychain.remote.AppSettings;
import org.sufficientlysecure.keychain.ui.base.BaseActivity;
import org.sufficientlysecure.keychain.ui.SelectPublicKeyFragment;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.util.Log;

import java.util.ArrayList;

// TODO: make extensible BaseRemoteServiceActivity and extend these cases from it
public class RemoteServiceActivity extends BaseActivity {

    public static final String ACTION_REGISTER = Constants.INTENT_PREFIX + "API_ACTIVITY_REGISTER";
    public static final String ACTION_CREATE_ACCOUNT = Constants.INTENT_PREFIX
            + "API_ACTIVITY_CREATE_ACCOUNT";
    public static final String ACTION_SELECT_PUB_KEYS = Constants.INTENT_PREFIX
            + "API_ACTIVITY_SELECT_PUB_KEYS";
    public static final String ACTION_ERROR_MESSAGE = Constants.INTENT_PREFIX
            + "API_ACTIVITY_ERROR_MESSAGE";

    public static final String EXTRA_MESSENGER = "messenger";

    public static final String EXTRA_DATA = "data";

    // passphrase action
    public static final String EXTRA_SECRET_KEY_ID = "secret_key_id";
    // register action
    public static final String EXTRA_PACKAGE_NAME = "package_name";
    public static final String EXTRA_PACKAGE_SIGNATURE = "package_signature";
    // create acc action
    public static final String EXTRA_ACC_NAME = "acc_name";
    // select pub keys action
    public static final String EXTRA_SELECTED_MASTER_KEY_IDS = "master_key_ids";
    public static final String EXTRA_MISSING_EMAILS = "missing_emails";
    public static final String EXTRA_DUPLICATE_EMAILS = "dublicate_emails";
    public static final String EXTRA_NO_USER_IDS_CHECK = "no_user_ids";
    // error message
    public static final String EXTRA_ERROR_MESSAGE = "error_message";

    // register view
    private AppSettingsHeaderFragment mAppSettingsHeaderFragment;
    // create acc view
    private AccountSettingsFragment mAccSettingsFragment;
    // select pub keys view
    private SelectPublicKeyFragment mSelectFragment;

    private ProviderHelper mProviderHelper;

    // for ACTION_CREATE_ACCOUNT
    boolean mUpdateExistingAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mProviderHelper = new ProviderHelper(this);

        handleActions(getIntent(), savedInstanceState);
    }

    @Override
    protected void initLayout() {
        // done in handleActions()
    }

    protected void handleActions(Intent intent, Bundle savedInstanceState) {

        String action = intent.getAction();
        final Bundle extras = intent.getExtras();


        switch (action) {
            case ACTION_REGISTER: {
                final String packageName = extras.getString(EXTRA_PACKAGE_NAME);
                final byte[] packageSignature = extras.getByteArray(EXTRA_PACKAGE_SIGNATURE);
                Log.d(Constants.TAG, "ACTION_REGISTER packageName: " + packageName);

                setContentView(R.layout.api_remote_register_app);
                initToolbar();

                mAppSettingsHeaderFragment = (AppSettingsHeaderFragment) getSupportFragmentManager().findFragmentById(
                        R.id.api_app_settings_fragment);

                AppSettings settings = new AppSettings(packageName, packageSignature);
                mAppSettingsHeaderFragment.setAppSettings(settings);

                // Inflate a "Done"/"Cancel" custom action bar view
                setFullScreenDialogTwoButtons(
                        R.string.api_register_allow, R.drawable.ic_check_white_24dp,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // Allow

                                mProviderHelper.insertApiApp(mAppSettingsHeaderFragment.getAppSettings());

                                // give data through for new service call
                                Intent resultData = extras.getParcelable(EXTRA_DATA);
                                RemoteServiceActivity.this.setResult(RESULT_OK, resultData);
                                RemoteServiceActivity.this.finish();
                            }
                        }, R.string.api_register_disallow, R.drawable.ic_close_white_24dp,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // Disallow
                                RemoteServiceActivity.this.setResult(RESULT_CANCELED);
                                RemoteServiceActivity.this.finish();
                            }
                        }
                );
                break;
            }
            case ACTION_CREATE_ACCOUNT: {
                final String packageName = extras.getString(EXTRA_PACKAGE_NAME);
                final String accName = extras.getString(EXTRA_ACC_NAME);

                setContentView(R.layout.api_remote_create_account);
                initToolbar();

                mAccSettingsFragment = (AccountSettingsFragment) getSupportFragmentManager().findFragmentById(
                        R.id.api_account_settings_fragment);

                TextView text = (TextView) findViewById(R.id.api_remote_create_account_text);

                // update existing?
                Uri uri = KeychainContract.ApiAccounts.buildByPackageAndAccountUri(packageName, accName);
                AccountSettings settings = mProviderHelper.getApiAccountSettings(uri);
                if (settings == null) {
                    // create new account
                    settings = new AccountSettings(accName);
                    mUpdateExistingAccount = false;

                    text.setText(R.string.api_create_account_text);
                } else {
                    // update existing account
                    mUpdateExistingAccount = true;

                    text.setText(R.string.api_update_account_text);
                }
                mAccSettingsFragment.setAccSettings(settings);

                // Inflate a "Done"/"Cancel" custom action bar view
                setFullScreenDialogDoneClose(R.string.api_settings_save,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // Save

                                // user needs to select a key if it has explicitly requested (None is only allowed for new accounts)
                                if (mUpdateExistingAccount && mAccSettingsFragment.getAccSettings().getKeyId() == Constants.key.none) {
                                    Notify.create(RemoteServiceActivity.this, getString(R.string.api_register_error_select_key), Notify.Style.ERROR).show();
                                } else {
                                    if (mUpdateExistingAccount) {
                                        Uri baseUri = KeychainContract.ApiAccounts.buildBaseUri(packageName);
                                        Uri accountUri = baseUri.buildUpon().appendEncodedPath(accName).build();
                                        mProviderHelper.updateApiAccount(
                                                accountUri,
                                                mAccSettingsFragment.getAccSettings());
                                    } else {
                                        mProviderHelper.insertApiAccount(
                                                KeychainContract.ApiAccounts.buildBaseUri(packageName),
                                                mAccSettingsFragment.getAccSettings());
                                    }

                                    // give data through for new service call
                                    Intent resultData = extras.getParcelable(EXTRA_DATA);
                                    RemoteServiceActivity.this.setResult(RESULT_OK, resultData);
                                    RemoteServiceActivity.this.finish();
                                }
                            }
                        },
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // Cancel
                                RemoteServiceActivity.this.setResult(RESULT_CANCELED);
                                RemoteServiceActivity.this.finish();
                            }
                        });

                break;
            }
            case ACTION_SELECT_PUB_KEYS: {
                long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS);
                boolean noUserIdsCheck = intent.getBooleanExtra(EXTRA_NO_USER_IDS_CHECK, true);
                ArrayList<String> missingEmails = intent
                        .getStringArrayListExtra(EXTRA_MISSING_EMAILS);
                ArrayList<String> duplicateEmails = intent
                        .getStringArrayListExtra(EXTRA_DUPLICATE_EMAILS);

                SpannableStringBuilder ssb = new SpannableStringBuilder();
                final SpannableString textIntro = new SpannableString(
                        noUserIdsCheck ? getString(R.string.api_select_pub_keys_text_no_user_ids)
                                : getString(R.string.api_select_pub_keys_text)
                );
                textIntro.setSpan(new StyleSpan(Typeface.BOLD), 0, textIntro.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                ssb.append(textIntro);

                if (missingEmails != null && missingEmails.size() > 0) {
                    ssb.append("\n\n");
                    ssb.append(getString(R.string.api_select_pub_keys_missing_text));
                    ssb.append("\n");
                    for (String emails : missingEmails) {
                        SpannableString ss = new SpannableString(emails + "\n");
                        ss.setSpan(new BulletSpan(15, Color.BLACK), 0, ss.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ssb.append(ss);
                    }
                }
                if (duplicateEmails != null && duplicateEmails.size() > 0) {
                    ssb.append("\n\n");
                    ssb.append(getString(R.string.api_select_pub_keys_dublicates_text));
                    ssb.append("\n");
                    for (String email : duplicateEmails) {
                        SpannableString ss = new SpannableString(email + "\n");
                        ss.setSpan(new BulletSpan(15, Color.BLACK), 0, ss.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ssb.append(ss);
                    }
                }

                setContentView(R.layout.api_remote_select_pub_keys);
                initToolbar();

                // Inflate a "Done"/"Cancel" custom action bar view
                setFullScreenDialogDoneClose(R.string.btn_okay,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // add key ids to params Bundle for new request
                                Intent resultData = extras.getParcelable(EXTRA_DATA);
                                resultData.putExtra(OpenPgpApi.EXTRA_KEY_IDS,
                                        mSelectFragment.getSelectedMasterKeyIds());

                                RemoteServiceActivity.this.setResult(RESULT_OK, resultData);
                                RemoteServiceActivity.this.finish();
                            }
                        },
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // cancel
                                RemoteServiceActivity.this.setResult(RESULT_CANCELED);
                                RemoteServiceActivity.this.finish();
                            }
                        });

                // set text on view
                TextView textView = (TextView) findViewById(R.id.api_select_pub_keys_text);
                textView.setText(ssb, TextView.BufferType.SPANNABLE);

                /* Load select pub keys fragment */
                // Check that the activity is using the layout version with
                // the fragment_container FrameLayout
                if (findViewById(R.id.api_select_pub_keys_fragment_container) != null) {

                    // However, if we're being restored from a previous state,
                    // then we don't need to do anything and should return or else
                    // we could end up with overlapping fragments.
                    if (savedInstanceState != null) {
                        return;
                    }

                    // Create an instance of the fragment
                    mSelectFragment = SelectPublicKeyFragment.newInstance(selectedMasterKeyIds);

                    // Add the fragment to the 'fragment_container' FrameLayout
                    getSupportFragmentManager().beginTransaction()
                            .add(R.id.api_select_pub_keys_fragment_container, mSelectFragment).commit();
                }
                break;
            }
            case ACTION_ERROR_MESSAGE: {
                String errorMessage = intent.getStringExtra(EXTRA_ERROR_MESSAGE);

                Spannable redErrorMessage = new SpannableString(errorMessage);
                redErrorMessage.setSpan(new ForegroundColorSpan(Color.RED), 0, errorMessage.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                setContentView(R.layout.api_remote_error_message);
                initToolbar();

                // Inflate a "Done" custom action bar view
                setFullScreenDialogClose(
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                RemoteServiceActivity.this.setResult(RESULT_CANCELED);
                                RemoteServiceActivity.this.finish();
                            }
                        }
                );

                // set text on view
                TextView textView = (TextView) findViewById(R.id.api_app_error_message_text);
                textView.setText(redErrorMessage);
                break;
            }
            default:
                Log.e(Constants.TAG, "Action does not exist!");
                setResult(RESULT_CANCELED);
                finish();
                break;
        }
    }
}