aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/theb/ssh/HostEditor.java
blob: d642fd4b24bed58a0017c3806afc0da14100b0a4 (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
package org.theb.ssh;

import org.theb.provider.HostDb;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.ContentURI;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class HostEditor extends Activity {
	public static final String EDIT_HOST_ACTION =
        "com.theb.ssh.action.EDIT_HOST";

    private static final String[] PROJECTION = new String[] {
    	HostDb.Hosts._ID, // 0
    	HostDb.Hosts.HOSTNAME, // 1
    	HostDb.Hosts.USERNAME, // 2
    	HostDb.Hosts.PORT, // 3
    	HostDb.Hosts.HOSTKEY, // 4
    };
    
    static final int HOSTNAME_INDEX = 1;
    private static final int USERNAME_INDEX = 2;
    private static final int PORT_INDEX = 3;
    // Set up distinct states that the activity can be run in.
    private static final int STATE_EDIT = 0;
    private static final int STATE_INSERT = 1;
    
	private EditText mHostname;
	private EditText mUsername;
	private EditText mPort;

	// Cursor that will provide access to the host data we are editing
    private Cursor mCursor;
    
	private int mState;
	private ContentURI mURI;
	
    @Override
    public void onCreate(Bundle savedValues) {
        super.onCreate(savedValues);
        
        // Have the system blur any windows behind this one.
        getWindow().setFlags(WindowManager.LayoutParams.BLUR_BEHIND_FLAG,
                WindowManager.LayoutParams.BLUR_BEHIND_FLAG);
        
        // Apply a tint to any windows behind this one.  Doing a tint this
        // way is more efficient than using a translucent background.  Note
        // that the tint color really should come from a resource.
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.tintBehind = 0x60000820;
        getWindow().setAttributes(lp);

        this.setContentView(R.layout.host_editor);
        
        // Set up click handlers for text fields and button
        mHostname = (EditText) findViewById(R.id.hostname);        
        mUsername = (EditText) findViewById(R.id.username);
        mPort = (EditText) findViewById(R.id.port);
        
        Button addButton = (Button) findViewById(R.id.add);
        addButton.setOnClickListener(mCommitListener);
        
        Button cancelButton = (Button) findViewById(R.id.cancel);
        cancelButton.setOnClickListener(mCancelListener);
        
        final Intent intent = getIntent();
        
        // Do some setup based on the action being performed.

        final String action = intent.getAction();
        if (Intent.INSERT_ACTION.equals(action)) {
        	mState = STATE_INSERT;
        	mURI = getContentResolver().insert(intent.getData(), null);
        
        	// If we were unable to create a new note, then just finish
        	// this activity.  A RESULT_CANCELED will be sent back to the
        	// original activity if they requested a result.
        	if (mURI == null) {
            	Log.e("Notes", "Failed to insert new note into "
                    + getIntent().getData());
            	finish();
            	return;
        	}
        	
        	// The new entry was created, so assume all will end well and
            // set the result to be returned.
            setResult(RESULT_OK, mURI.toString());
        } else {
        	// Editing is the default state.
       		mState = STATE_EDIT;
        	
        	// Get the URI of the host whose properties we want to edit
            mURI = getIntent().getData();
            
            // If were editing, change the Ok button to be Change instead.
            addButton.setText(R.string.button_change);
        }
        
        // Get a cursor to access the host data
        mCursor = managedQuery(mURI, PROJECTION, null, null);
    }

    @Override
    protected void onResume() {
    	super.onResume();
    	
    	// Initialize the text with the host data
    	if (mCursor != null) {
    		mCursor.first();
    		
    		String hostname = mCursor.getString(HOSTNAME_INDEX);
    		mHostname.setText(hostname);
    		
    		String username = mCursor.getString(USERNAME_INDEX);
    		mUsername.setText(username);
    		
    		String port = mCursor.getString(PORT_INDEX);
    		mPort.setText(port);
    	}
    }

    @Override
    protected void onPause() {
    	super.onPause();
    	
    	// Write the text back into the cursor
    	if (mCursor != null) {
    		String hostname = mHostname.getText().toString();
    		mCursor.updateString(HOSTNAME_INDEX, hostname);
    		
    		String username = mUsername.getText().toString();
    		mCursor.updateString(USERNAME_INDEX, username);
    		
    		String portStr = mPort.getText().toString();
    		int port = Integer.parseInt(portStr);
    		mCursor.updateInt(PORT_INDEX, port);
    		
    		if (isFinishing()
    				&& ((hostname.length() == 0)
    						|| (username.length() == 0)
    						|| (port == 0))) {
    			setResult(RESULT_CANCELED);
    			deleteHost();
    		} else {
    			managedCommitUpdates(mCursor);
    		}
    	}
    }
    
    private final void cancelEdit() {
    	if (mCursor != null) {
    		if (mState == STATE_EDIT) {
    			mCursor.deactivate();
    			mCursor = null;
    		} else if (mState == STATE_INSERT) {
    			deleteHost();
    		}
    	}
    }
    
    private final void deleteHost() {
    	if (mCursor != null) {
    		mHostname.setText("");
    		mUsername.setText("");
    		mPort.setText("");
    		mCursor.deleteRow();
    		mCursor.deactivate();
    		mCursor = null;
    	}
    }
    
    OnClickListener mCommitListener = new OnClickListener() {
    	public void onClick(View v) {
    		// When the user clicks, just finish this activity.
    		// onPause will be called, and we save our data there.
    		finish();
    	}
    };
    
    OnClickListener mCancelListener = new OnClickListener() {
    	public void onClick(View v) {
    		cancelEdit();
    		finish();
    	}
    };
}