aboutsummaryrefslogtreecommitdiffstats
path: root/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java
blob: 170b459be28775ff559a655835b894dc8c01bcfc (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
/*
 * Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
 * Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
 *
 * 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.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.ui.widget.ImportKeysListLoader;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.R;

import com.actionbarsherlock.app.SherlockListFragment;

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

public class ImportKeysListFragment extends SherlockListFragment implements
        LoaderManager.LoaderCallbacks<List<Map<String, String>>> {
    public static String ARG_KEYRING_BYTES = "bytes";
    public static String ARG_IMPORT_FILENAME = "filename";

    byte[] mKeyringBytes;
    String mImportFilename;

    private Activity mActivity;
    private SimpleAdapter mAdapter;

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        // Map<String, String> item = (Map<String, String>) listView.getItemAtPosition(position);
        // String userId = item.get(ImportKeysListLoader.MAP_ATTR_USER_ID);
    }

    /**
     * Resume is called after rotating
     */
    @Override
    public void onResume() {
        super.onResume();

        // Start out with a progress indicator.
        setListShown(false);

        // reload list
        getLoaderManager().restartLoader(0, null, this);
    }

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

        mActivity = this.getActivity();

        mKeyringBytes = getArguments().getByteArray(ARG_KEYRING_BYTES);
        mImportFilename = getArguments().getString(ARG_IMPORT_FILENAME);

        // register long press context menu
        registerForContextMenu(getListView());

        // 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_nothingImport));

        // Create an empty adapter we will use to display the loaded data.
        String[] from = new String[] {};
        int[] to = new int[] {};
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        mAdapter = new SimpleAdapter(getActivity(), data, android.R.layout.two_line_list_item,
                from, to);
        setListAdapter(mAdapter);

        // Start out with a progress indicator.
        setListShown(false);

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

    @Override
    public Loader<List<Map<String, String>>> onCreateLoader(int id, Bundle args) {
        return new ImportKeysListLoader(mActivity, mKeyringBytes, mImportFilename);
    }

    @Override
    public void onLoadFinished(Loader<List<Map<String, String>>> loader,
            List<Map<String, String>> data) {
        // Set the new data in the adapter.
        // for (String item : data) {
        // mAdapter.add(item);
        // }
        Log.d(Constants.TAG, "data: " + data);
        // TODO: real swapping the data to the already defined adapter doesn't work
        // Workaround: recreate adapter!
        // http://stackoverflow.com/questions/2356091/android-add-function-of-arrayadapter-not-working
        // mAdapter = new ArrayAdapter<String>(mActivity, android.R.layout.two_line_list_item,
        // data);

        String[] from = new String[] { ImportKeysListLoader.MAP_ATTR_USER_ID,
                ImportKeysListLoader.MAP_ATTR_FINGERPINT };
        int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
        mAdapter = new SimpleAdapter(getActivity(), data, android.R.layout.two_line_list_item,
                from, to);

        mAdapter.notifyDataSetChanged();
        setListAdapter(mAdapter);

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

    @Override
    public void onLoaderReset(Loader<List<Map<String, String>>> loader) {
        // Clear the data in the adapter.
        // Not available in SimpleAdapter!
        // mAdapter.clear();
    }

}