aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java
blob: c1aa8a1f2ae82da8c0700c2730cad24483641cad (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
346
347
348
349
350
351
352
353
/*
 * Copyright (C) 2012-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.ui;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;

import com.devspark.appmsg.AppMsg;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.ui.adapter.AsyncTaskResultWrapper;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysAdapter;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListKeybaseLoader;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListLoader;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListServerLoader;
import org.sufficientlysecure.keychain.util.InputData;
import org.sufficientlysecure.keychain.util.KeyServer;
import org.sufficientlysecure.keychain.util.Log;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ImportKeysListFragment extends ListFragment implements
        LoaderManager.LoaderCallbacks<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> {
    private static final String ARG_DATA_URI = "uri";
    private static final String ARG_BYTES = "bytes";
    private static final String ARG_SERVER_QUERY = "query";

    private Activity mActivity;
    private ImportKeysAdapter mAdapter;

    private byte[] mKeyBytes;
    private Uri mDataUri;
    private String mServerQuery;
    private String mKeyServer;
    private String mKeybaseQuery;

    private static final int LOADER_ID_BYTES = 0;
    private static final int LOADER_ID_SERVER_QUERY = 1;
    private static final int LOADER_ID_KEYBASE = 2;

    public byte[] getKeyBytes() {
        return mKeyBytes;
    }

    public Uri getDataUri() {
        return mDataUri;
    }

    public String getServerQuery() {
        return mServerQuery;
    }

    public String getKeybaseQuery() {
        return mKeybaseQuery;
    }

    public String getKeyServer() {
        return mKeyServer;
    }

    public List<ImportKeysListEntry> getData() {
        return mAdapter.getData();
    }

    public ArrayList<ImportKeysListEntry> getSelectedData() {
        return mAdapter.getSelectedData();
    }

    /**
     * Creates new instance of this fragment
     */
    public static ImportKeysListFragment newInstance(byte[] bytes, Uri dataUri, String serverQuery) {
        ImportKeysListFragment frag = new ImportKeysListFragment();

        Bundle args = new Bundle();
        args.putByteArray(ARG_BYTES, bytes);
        args.putParcelable(ARG_DATA_URI, dataUri);
        args.putString(ARG_SERVER_QUERY, serverQuery);

        frag.setArguments(args);

        return frag;
    }

    /**
     * Define Adapter and Loader on create of Activity
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mActivity = getActivity();

        // Give some text to display if there is no data. In a real
        // application this would come from a resource.
        setEmptyText(mActivity.getString(R.string.error_nothing_import));

        // Create an empty adapter we will use to display the loaded data.
        mAdapter = new ImportKeysAdapter(mActivity);
        setListAdapter(mAdapter);

        mDataUri = getArguments().getParcelable(ARG_DATA_URI);
        mKeyBytes = getArguments().getByteArray(ARG_BYTES);
        mServerQuery = getArguments().getString(ARG_SERVER_QUERY);

        // TODO: this is used when scanning QR Code. Currently it simply uses keyserver nr 0
        mKeyServer = Preferences.getPreferences(getActivity())
                .getKeyServers()[0];

        if (mDataUri != null || mKeyBytes != null) {
            // Start out with a progress indicator.
            setListShown(false);

            // Prepare the loader. Either re-connect with an existing one,
            // or start a new one.
            // give arguments to onCreateLoader()
            getLoaderManager().initLoader(LOADER_ID_BYTES, null, this);
        }

        if (mServerQuery != null && mKeyServer != null) {
            // Start out with a progress indicator.
            setListShown(false);

            // Prepare the loader. Either re-connect with an existing one,
            // or start a new one.
            // give arguments to onCreateLoader()
            getLoaderManager().initLoader(LOADER_ID_SERVER_QUERY, null, this);
        }

        if (mKeybaseQuery != null) {
            // Start out with a progress indicator.
            setListShown(false);

            // Prepare the loader. Either re-connect with an existing one,
            // or start a new one.
            // give arguments to onCreateLoader()
            getLoaderManager().initLoader(LOADER_ID_KEYBASE, null, this);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        // Select checkbox!
        // Update underlying data and notify adapter of change. The adapter will
        // update the view automatically.

        ImportKeysListEntry entry = mAdapter.getItem(position);
        entry.setSelected(!entry.isSelected());
        mAdapter.notifyDataSetChanged();
    }

    public void loadNew(byte[] keyBytes, Uri dataUri, String serverQuery, String keyServer, String keybaseQuery) {
        mKeyBytes = keyBytes;
        mDataUri = dataUri;
        mServerQuery = serverQuery;
        mKeyServer = keyServer;
        mKeybaseQuery = keybaseQuery;

        if (mKeyBytes != null || mDataUri != null) {
            // Start out with a progress indicator.
            setListShown(false);

            getLoaderManager().restartLoader(LOADER_ID_BYTES, null, this);
        }

        if (mServerQuery != null && mKeyServer != null) {
            // Start out with a progress indicator.
            setListShown(false);

            getLoaderManager().restartLoader(LOADER_ID_SERVER_QUERY, null, this);
        }

        if (mKeybaseQuery != null) {
            // Start out with a progress indicator.
            setListShown(false);

            getLoaderManager().restartLoader(LOADER_ID_KEYBASE, null, this);
        }
    }

    @Override
    public Loader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>>
                                        onCreateLoader(int id, Bundle args) {
        switch (id) {
            case LOADER_ID_BYTES: {
                InputData inputData = getInputData(mKeyBytes, mDataUri);
                return new ImportKeysListLoader(mActivity, inputData);
            }
            case LOADER_ID_SERVER_QUERY: {
                return new ImportKeysListServerLoader(getActivity(), mServerQuery, mKeyServer);
            }
            case LOADER_ID_KEYBASE: {
                return new ImportKeysListKeybaseLoader(getActivity(), mKeybaseQuery);
            }

            default:
                return null;
        }
    }

    @Override
    public void onLoadFinished(Loader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> loader,
                               AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> data) {
        // Swap the new cursor in. (The framework will take care of closing the
        // old cursor once we return.)

        Log.d(Constants.TAG, "data: " + data.getResult());

        // swap in the real data!
        mAdapter.setData(data.getResult());
        mAdapter.notifyDataSetChanged();

        setListAdapter(mAdapter);

        // The list should now be shown.
        if (isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }

        Exception error = data.getError();

        switch (loader.getId()) {
            case LOADER_ID_BYTES:

                if (error == null) {
                    // No error
                } else if (error instanceof ImportKeysListLoader.FileHasNoContent) {
                    AppMsg.makeText(getActivity(), R.string.error_import_file_no_content,
                            AppMsg.STYLE_ALERT).show();
                } else if (error instanceof ImportKeysListLoader.NonPgpPart) {
                    AppMsg.makeText(getActivity(),
                            ((ImportKeysListLoader.NonPgpPart) error).getCount() + " " + getResources().
                                    getQuantityString(R.plurals.error_import_non_pgp_part,
                                            ((ImportKeysListLoader.NonPgpPart) error).getCount()),
                            new AppMsg.Style(AppMsg.LENGTH_LONG, R.color.confirm)).show();
                } else {
                    AppMsg.makeText(getActivity(), R.string.error_generic_report_bug,
                            new AppMsg.Style(AppMsg.LENGTH_LONG, R.color.alert)).show();
                }
                break;

            case LOADER_ID_SERVER_QUERY:

                if (error == null) {
                    AppMsg.makeText(
                            getActivity(), getResources().getQuantityString(R.plurals.keys_found,
                                    mAdapter.getCount(), mAdapter.getCount()),
                            AppMsg.STYLE_INFO
                    ).show();
                } else if (error instanceof KeyServer.InsufficientQuery) {
                    AppMsg.makeText(getActivity(), R.string.error_keyserver_insufficient_query,
                            AppMsg.STYLE_ALERT).show();
                } else if (error instanceof KeyServer.QueryException) {
                    AppMsg.makeText(getActivity(), R.string.error_keyserver_query,
                            AppMsg.STYLE_ALERT).show();
                } else if (error instanceof KeyServer.TooManyResponses) {
                    AppMsg.makeText(getActivity(), R.string.error_keyserver_too_many_responses,
                            AppMsg.STYLE_ALERT).show();
                }
                break;

            case LOADER_ID_KEYBASE:

                if (error == null) {
                    AppMsg.makeText(
                            getActivity(), getResources().getQuantityString(R.plurals.keys_found,
                                    mAdapter.getCount(), mAdapter.getCount()),
                            AppMsg.STYLE_INFO
                    ).show();
                }  else if (error instanceof KeyServer.QueryException) {
                    AppMsg.makeText(getActivity(), R.string.error_keyserver_query,
                            AppMsg.STYLE_ALERT).show();
                }

            default:
                break;
        }
    }

    @Override
    public void onLoaderReset(Loader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> loader) {
        switch (loader.getId()) {
            case LOADER_ID_BYTES:
                // Clear the data in the adapter.
                mAdapter.clear();
                break;
            case LOADER_ID_SERVER_QUERY:
                // Clear the data in the adapter.
                mAdapter.clear();
                break;
            case LOADER_ID_KEYBASE:
                // Clear the data in the adapter.
                mAdapter.clear();
                break;
            default:
                break;
        }
    }

    private InputData getInputData(byte[] importBytes, Uri dataUri) {
        InputData inputData = null;
        if (importBytes != null) {
            inputData = new InputData(new ByteArrayInputStream(importBytes), importBytes.length);
        } else if (dataUri != null) {
            try {
                InputStream inputStream = getActivity().getContentResolver().openInputStream(dataUri);
                int length = inputStream.available();

                inputData = new InputData(inputStream, length);
            } catch (FileNotFoundException e) {
                Log.e(Constants.TAG, "FileNotFoundException!", e);
            } catch (IOException e) {
                Log.e(Constants.TAG, "IOException!", e);
            }
        }

        return inputData;
    }

}