aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/UserPreference.java
blob: becf44ea583bc47703ca6c9c319ea48f1f979328 (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 com.zeapo.pwdstore;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.Toast;

import com.zeapo.pwdstore.crypto.PgpHandler;
import com.zeapo.pwdstore.git.GitActivity;
import com.zeapo.pwdstore.utils.PasswordRepository;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class UserPreference extends ActionBarActivity implements Preference.OnPreferenceClickListener {
    private final static int IMPORT_SSH_KEY = 1;
    private final static int IMPORT_PGP_KEY = 2;
    private final static int EDIT_GIT_INFO = 3;

    public static class PrefsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preference);
            Preference keyPref = findPreference("openpgp_key_id");
            keyPref.setSummary(getPreferenceManager().getSharedPreferences().getString("openpgp_key_ids", "No key selected"));
            keyPref.setOnPreferenceClickListener((UserPreference) getActivity());
            findPreference("ssh_key").setOnPreferenceClickListener((UserPreference) getActivity());
            findPreference("git_server_info").setOnPreferenceClickListener((UserPreference) getActivity());
            findPreference("git_delete_repo").setOnPreferenceClickListener((UserPreference) getActivity());
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent() != null) {
            if ((getIntent().getStringExtra("operation") != null) && (getIntent().getStringExtra("operation").equals("get_ssh_key"))) {
                getSshKey();
            }
        }

        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new PrefsFragment()).commit();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                setResult(RESULT_OK);
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Opens a file explorer to import the private key
     */
    public void getSshKey() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivityForResult(intent, IMPORT_SSH_KEY);
    }


    private void copySshKey(Uri uri) throws IOException {
        InputStream sshKey = this.getContentResolver().openInputStream(uri);
        byte[] privateKey = IOUtils.toByteArray(sshKey);
        FileUtils.writeByteArrayToFile(new File(getFilesDir() + "/.ssh_key"), privateKey);
        sshKey.close();
    }

    @Override
    public boolean onPreferenceClick(Preference pref) {
        switch (pref.getKey())
        {
            case "openpgp_key_id":
            {
                Intent intent = new Intent(this, PgpHandler.class);
                intent.putExtra("Operation", "GET_KEY_ID");
                startActivityForResult(intent, IMPORT_PGP_KEY);
            }
            break;
            case "ssh_key":
            {
                getSshKey();
            }
            break;
            case "git_server_info":
            {
                Intent intent = new Intent(this, GitActivity.class);
                intent.putExtra("Operation", GitActivity.EDIT_SERVER);
                startActivityForResult(intent, EDIT_GIT_INFO);
            }
            break;
            case "git_delete_repo":
            {
                new AlertDialog.Builder(this).
                        setTitle(R.string.pref_dialog_delete_title).
                        setMessage(R.string.pref_dialog_delete_msg).
                        setCancelable(false).
                        setPositiveButton(R.string.dialog_delete,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        try {
                                            FileUtils.deleteDirectory(PasswordRepository.getWorkTree());
                                        } catch (Exception e) {
                                            //TODO Handle the diffent cases of exceptions
                                        }

                                        PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("repository_initialized", false).commit();
                                        dialog.cancel();
                                        finish();
                                    }
                                }
                        ).
                        setNegativeButton(R.string.dialog_do_not_delete,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                }
                        ).
                        show();
            }
        }
        return true;
    }

    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode)
            {
                case IMPORT_SSH_KEY:
                {
                    try {
                        if (data.getData() == null) {
                            throw new IOException("Unable to open file");
                        }
                        copySshKey(data.getData());
                        Toast.makeText(this, this.getResources().getString(R.string.ssh_key_success_dialog_title), Toast.LENGTH_LONG).show();
                        setResult(RESULT_OK);
                        finish();
                    } catch (IOException e)
                    {
                        new AlertDialog.Builder(this).
                                setTitle(this.getResources().getString(R.string.ssh_key_error_dialog_title)).
                                setMessage(this.getResources().getString(R.string.ssh_key_error_dialog_text) + e.getMessage()).
                                setPositiveButton(this.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        //pass
                                    }
                                }).show();
                    }
                }
                break;
                case EDIT_GIT_INFO:
                {

                }
                break;
                default:
                break;
            }
        }
    }
}