aboutsummaryrefslogtreecommitdiffstats
path: root/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/remote_api/AppSettingsActivity.java
blob: 3046dd0d29cd731a4772aa3838c137313539a9c1 (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
package org.sufficientlysecure.keychain.remote_api;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.util.Log;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class AppSettingsActivity extends SherlockFragmentActivity {
    private PackageManager pm;

    // model
    Uri appUri;
    long id;
    String packageName;
    long keyId;
    boolean asciiArmor;

    // model, derived
    String appName;

    // view
    TextView selectedKey;
    Button selectKeyButton;
    CheckBox asciiArmorCheckBox;
    Button saveButton;
    Button revokeButton;

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

        pm = getApplicationContext().getPackageManager();

        setContentView(R.layout.api_app_settings_activity);

        selectedKey = (TextView) findViewById(R.id.api_app_settings_selected_key);
        selectKeyButton = (Button) findViewById(R.id.api_app_settings_select_key_button);
        asciiArmorCheckBox = (CheckBox) findViewById(R.id.api_app_ascii_armor);
        revokeButton = (Button) findViewById(R.id.api_app_settings_revoke);
        saveButton = (Button) findViewById(R.id.api_app_settings_save);

        revokeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                revokeAccess();
            }
        });
        saveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                save();
            }
        });

        Intent intent = getIntent();
        appUri = intent.getData();
        if (appUri == null) {
            Log.e(Constants.TAG, "Intent data missing. Should be Uri of app!");
            finish();
            return;
        } else {
            Log.d(Constants.TAG, "uri: " + appUri);
            loadData(appUri);
        }
    }

    private void loadData(Uri appUri) {
        Cursor cur = getContentResolver().query(appUri, null, null, null, null);
        id = ContentUris.parseId(appUri);
        if (cur.moveToFirst()) {
            do {
                packageName = cur.getString(cur
                        .getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME));
                // get application name
                try {
                    ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);

                    appName = (String) pm.getApplicationLabel(ai);
                } catch (final NameNotFoundException e) {
                    appName = getString(R.string.api_unknown_app);
                }

                try {
                    asciiArmor = (cur.getInt(cur
                            .getColumnIndexOrThrow(KeychainContract.ApiApps.ASCII_ARMOR)) == 1);

                    // display values
                    asciiArmorCheckBox.setChecked(asciiArmor);
                } catch (IllegalArgumentException e) {
                    Log.e(Constants.TAG, "AppSettingsActivity", e);
                }

            } while (cur.moveToNext());
        }
    }

    private void revokeAccess() {
        Uri calUri = ContentUris.withAppendedId(appUri, id);
        getContentResolver().delete(calUri, null, null);
        finish();
    }

    private void save() {
        final ContentValues cv = new ContentValues();
        cv.put(KeychainContract.ApiApps.PACKAGE_NAME, packageName);
        cv.put(KeychainContract.ApiApps.ASCII_ARMOR, asciiArmor);
        // TODO: other parameter
        getContentResolver().update(appUri, cv, null, null);

        finish();
    }

}