aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/theb/ssh/HostsList.java
blob: 2634eba3ffe3f554f921b9630c454342dfba86de (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright (C) 2007 Kenny Root (kenny at the-b.org)
 * 
 * This file is part of Connectbot.
 *
 *  Connectbot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Connectbot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Connectbot.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.theb.ssh;

import org.theb.provider.HostDb;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.ContentURI;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.SubMenu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.MeasureSpec;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class HostsList extends ListActivity {
	public static final int DELETE_ID = Menu.FIRST;
	public static final int INSERT_ID = Menu.FIRST + 1;
	public static final int PREFERENCES_ID = Menu.FIRST + 2;
	public static final int ABOUT_ID = Menu.FIRST + 3;
	
	// Preferences submenu
	public static final int PUBKEY_ID = SubMenu.FIRST + 4;
	
	private static final String[] PROJECTION = new String[] {
		HostDb.Hosts._ID,
		HostDb.Hosts.HOSTNAME,
		HostDb.Hosts.USERNAME, 
		HostDb.Hosts.PORT,
	};
	
	private Cursor mCursor;
	
	/**
	 * @author kenny
	 * Imparts a more informative view of the host list.
	 * 
	 * Displays as "username@hostname:port" but only includes the port if it is
	 * not on the default port 22.
	 */
	public class HostListCursorAdapter extends SimpleCursorAdapter {

		public HostListCursorAdapter(Context context, int layout, Cursor c,
				String[] from, int[] to) {
			super(context, layout, c, from, to);
		}
		
		@Override
		 public void bindView(View view, Context context, Cursor cursor) {
			String label;
			TextView textView = (TextView) view;

			label = cursor.getString(2)
					+ "@"
					+ cursor.getString(1);
			
			int port = cursor.getInt(3);
			if (port != 22) {
				label = label + ":" + String.valueOf(port);
			}
			
			textView.setText(label);
		}
		
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);	
        
        setDefaultKeyMode(SHORTCUT_DEFAULT_KEYS);
        
        Intent intent = getIntent();
        if (intent.getData() == null) {
        	intent.setData(HostDb.Hosts.CONTENT_URI);
        }
        
        setupListStripes();
        
        mCursor = managedQuery(getIntent().getData(), PROJECTION, null, null);

        ListAdapter adapter = new HostListCursorAdapter(this,
                android.R.layout.simple_list_item_1, mCursor,
                new String[] {HostDb.Hosts.HOSTNAME}, new int[] {android.R.id.text1});

        setListAdapter(adapter);
    }
    
    /**
     * Add stripes to the list view.
     */
    private void setupListStripes() {
        // Get Drawables for alternating stripes
        Drawable[] lineBackgrounds = new Drawable[2];
        
        lineBackgrounds[0] = getResources().getDrawable(R.drawable.even_stripe);
        lineBackgrounds[1] = getResources().getDrawable(R.drawable.odd_stripe);

        // Make and measure a sample TextView of the sort our adapter will
        // return
        View view = getViewInflate().inflate(
                android.R.layout.simple_list_item_1, null, null);

        TextView v = (TextView) view.findViewById(android.R.id.text1);
        v.setText("X");
        // Make it 100 pixels wide, and let it choose its own height.
        v.measure(MeasureSpec.makeMeasureSpec(View.MeasureSpec.EXACTLY, 100),
                MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, 0));
        int height = v.getMeasuredHeight();
        getListView().setStripes(lineBackgrounds, height);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // This is our one standard application action -- inserting a
        // new host into the list.
        menu.add(0, INSERT_ID, R.string.menu_insert).setShortcut(
                KeyEvent.KEYCODE_3, 0, KeyEvent.KEYCODE_A);

        // The preferences link allows users to e.g. set the pubkey
        SubMenu prefs = menu.addSubMenu(0, 0, R.string.menu_preferences);
        prefs.add(0, PUBKEY_ID, R.string.menu_pubkey).setShortcut(
        		KeyEvent.KEYCODE_4, 0, KeyEvent.KEYCODE_P);
        
        // This links to the about dialog for the program.
        menu.add(0, ABOUT_ID, R.string.menu_about);
        
        // Generate any additional actions that can be performed on the
        // overall list.  In a normal install, there are no additional
        // actions found here, but this allows other applications to extend
        // our menu with their own actions.
        Intent intent = new Intent(null, getIntent().getData());
        intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
        menu.addIntentOptions(
            Menu.ALTERNATIVE, 0, new ComponentName(this, HostsList.class),
            null, intent, 0, null);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        final boolean haveItems = mCursor.count() > 0;

        // If there are any notes in the list (which implies that one of
        // them is selected), then we need to generate the actions that
        // can be performed on the current selection.  This will be a combination
        // of our own specific actions along with any extensions that can be
        // found.
        if (haveItems) {
            // This is the selected item.
            ContentURI uri = getIntent().getData().addId(getSelectionRowID());

            // Build menu...  always starts with the PICK action...
            Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.PICK_ACTION, uri);
            Menu.Item[] items = new Menu.Item[1];

            // ... is followed by whatever other actions are available...
            Intent intent = new Intent(null, uri);
            intent.addCategory(Intent.SELECTED_ALTERNATIVE_CATEGORY);
            menu.addIntentOptions(Menu.SELECTED_ALTERNATIVE, 0, null, specifics,
                                  intent, Menu.NO_SEPARATOR_AFTER, items);

            // ... and ends with the delete command.
            menu.add(Menu.SELECTED_ALTERNATIVE, DELETE_ID, R.string.menu_delete)
.
            setShortcut(KeyEvent.KEYCODE_2, 0, KeyEvent.KEYCODE_D);
            menu.addSeparator(Menu.SELECTED_ALTERNATIVE, 0);

            // Give a shortcut to the connect action.
            if (items[0] != null) {
                items[0].setShortcut(KeyEvent.KEYCODE_1, 0, KeyEvent.KEYCODE_C);
            }
        } else {
            menu.removeGroup(Menu.SELECTED_ALTERNATIVE);
        }

        // Make sure the delete action is disabled if there are no items.
        menu.setItemShown(DELETE_ID, haveItems);
        return true;
    }
  
    @Override
    public boolean onOptionsItemSelected(Menu.Item item) {
        switch (item.getId()) {
        case DELETE_ID:
            deleteItem();
            return true;
        case INSERT_ID:
            insertItem();
            return true;
        case PUBKEY_ID:
        	showPubkey();
        	return true;
        case ABOUT_ID:
        	showAbout();
        	return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private void showPubkey() {
    	Intent intent = new Intent(this, Pubkey.class);
    	this.startActivity(intent);
	}

	private void showAbout() {
		Dialog about = new Dialog(this);
		about.setContentView(R.layout.about_dialog);
		about.setTitle(getResources().getString(R.string.app_name)
				+ " "
				+ getResources().getString(R.string.msg_version));
		
		// Everything looks cooler when you blur the window behind it.
        about.getWindow().setFlags(WindowManager.LayoutParams.BLUR_BEHIND_FLAG,
                WindowManager.LayoutParams.BLUR_BEHIND_FLAG);
        WindowManager.LayoutParams lp = about.getWindow().getAttributes();
        lp.tintBehind = 0x60000820;
        about.getWindow().setAttributes(lp);
        
		about.show();
	}

	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        ContentURI url = getIntent().getData().addId(getSelectionRowID());
        
        String action = getIntent().getAction();
        if (Intent.PICK_ACTION.equals(action)
                || Intent.GET_CONTENT_ACTION.equals(action)) {
            // The caller is waiting for us to return a note selected by
            // the user.  The have clicked on one, so return it now.
            setResult(RESULT_OK, url.toString());
        } else {
            // Launch activity to view/edit the currently selected item
            startActivity(new Intent(Intent.PICK_ACTION, url));
        }
    }

    private final void deleteItem() {
        mCursor.moveTo(getSelection());
        mCursor.deleteRow();
    }

    private final void insertItem() {
        // Launch activity to insert a new item
        startActivity(new Intent(Intent.INSERT_ACTION, getIntent().getData()));
    }
}