aboutsummaryrefslogtreecommitdiffstats
path: root/org_apg/src/org/thialfihar/android/apg/ui/KeyListPublicActivity.java
blob: b8ee2e8013963dd0d5b014e582ac0f0f135e60a6 (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
package org.thialfihar.android.apg.ui;

import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.PGPHelper;
import org.thialfihar.android.apg.provider.ProviderHelper;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

public class KeyListPublicActivity extends KeyListActivity {

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

        setContentView(R.layout.key_list_public_activity);

        mExportFilename = Constants.path.APP_DIR + "/pubexport.asc";
        mKeyType = Id.type.public_key;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1, Id.menu.option.search, 0, R.string.menu_search)
                .setIcon(R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(1, Id.menu.option.scanQRCode, 1, R.string.menu_scanQRCode)
                .setIcon(R.drawable.ic_menu_scan_qrcode)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menu.add(1, Id.menu.option.key_server, 2, R.string.menu_keyServer)
                .setIcon(R.drawable.ic_menu_search_list)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menu.add(0, Id.menu.option.import_keys, 3, R.string.menu_importKeys).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menu.add(0, Id.menu.option.export_keys, 4, R.string.menu_exportKeys).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case Id.menu.option.key_server: {
            startActivity(new Intent(this, KeyServerQueryActivity.class));

            return true;
        }
        case Id.menu.option.scanQRCode: {
            Intent intent = new Intent(this, ImportFromQRCodeActivity.class);
            intent.setAction(ImportFromQRCodeActivity.IMPORT_FROM_QR_CODE);
            startActivityForResult(intent, Id.request.import_from_qr_code);

            return true;
        }

        default: {
            return super.onOptionsItemSelected(item);
        }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
        int type = ExpandableListView.getPackedPositionType(info.packedPosition);

        if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            // TODO: user id? menu.setHeaderTitle("Key");
            menu.add(0, Id.menu.export, 0, R.string.menu_exportKey);
            menu.add(0, Id.menu.delete, 1, R.string.menu_deleteKey);
            menu.add(0, Id.menu.update, 1, R.string.menu_updateKey);
            menu.add(0, Id.menu.exportToServer, 1, R.string.menu_exportKeyToServer);
            menu.add(0, Id.menu.signKey, 1, R.string.menu_signKey);
        }
    }

    @Override
    public boolean onContextItemSelected(android.view.MenuItem menuItem) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);

        if (type != ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            return super.onContextItemSelected(menuItem);
        }

        switch (menuItem.getItemId()) {
        case Id.menu.update: {
            mSelectedItem = groupPosition;
            final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
            long keyId = 0;
            PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(this,
                    keyRingId);
            if (keyRing != null) {
                keyId = PGPHelper.getMasterKey((PGPPublicKeyRing) keyRing).getKeyID();
            }
            if (keyId == 0) {
                // this shouldn't happen
                return true;
            }

            Intent intent = new Intent(this, KeyServerQueryActivity.class);
            intent.setAction(KeyServerQueryActivity.ACTION_LOOK_UP_KEY_ID_AND_RETURN);
            intent.putExtra(KeyServerQueryActivity.EXTRA_KEY_ID, keyId);
            startActivityForResult(intent, Id.request.look_up_key_id);

            return true;
        }

        case Id.menu.exportToServer: {
            mSelectedItem = groupPosition;
            final int keyRingId = mListAdapter.getKeyRingId(groupPosition);

            Intent intent = new Intent(this, KeyServerUploadActivity.class);
            intent.setAction(KeyServerUploadActivity.ACTION_EXPORT_KEY_TO_SERVER);
            intent.putExtra(KeyServerUploadActivity.EXTRA_KEY_ID, keyRingId);
            startActivityForResult(intent, Id.request.export_to_server);

            return true;
        }

        case Id.menu.signKey: {
            mSelectedItem = groupPosition;
            final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
            long keyId = 0;
            PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(this,
                    keyRingId);
            if (keyRing != null) {
                keyId = PGPHelper.getMasterKey((PGPPublicKeyRing) keyRing).getKeyID();
            }

            if (keyId == 0) {
                // this shouldn't happen
                return true;
            }

            Intent intent = new Intent(this, SignKeyActivity.class);
            intent.putExtra(SignKeyActivity.EXTRA_KEY_ID, keyId);
            startActivity(intent);

            return true;
        }

        default: {
            return super.onContextItemSelected(menuItem);
        }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case Id.request.look_up_key_id: {
            if (resultCode == RESULT_CANCELED || data == null
                    || data.getStringExtra(KeyServerQueryActivity.RESULT_EXTRA_TEXT) == null) {
                return;
            }

            Intent intent = new Intent(this, KeyListPublicActivity.class);
            intent.setAction(KeyListPublicActivity.ACTION_IMPORT);
            intent.putExtra(KeyListPublicActivity.EXTRA_TEXT,
                    data.getStringExtra(KeyListActivityOld.EXTRA_TEXT));
            handleIntent(intent);
            break;
        }

        default: {
            super.onActivityResult(requestCode, resultCode, data);
            break;
        }
        }
    }
}