/WebVox/src/com/marvin/webvox/ScriptListActivity.java
Java | 196 lines | 133 code | 34 blank | 29 comment | 6 complexity | b33db75b15357537bf41f842104f64aa MD5 | raw file
1/* 2 * Copyright (C) 2008 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.marvin.webvox; 18 19import java.io.File; 20import java.io.FileFilter; 21import java.io.FilenameFilter; 22import java.util.Collections; 23import java.util.LinkedList; 24import java.util.List; 25 26import android.app.Activity; 27import android.app.AlertDialog; 28import android.app.ListActivity; 29import android.app.SearchManager; 30import android.content.DialogInterface; 31import android.content.Intent; 32import android.content.DialogInterface.OnClickListener; 33import android.database.Cursor; 34import android.net.Uri; 35import android.os.Bundle; 36import android.os.Environment; 37import android.util.Log; 38import android.view.ContextMenu; 39import android.view.Menu; 40import android.view.MenuItem; 41import android.view.View; 42import android.view.MenuItem.OnMenuItemClickListener; 43import android.widget.AdapterView; 44import android.widget.CheckBox; 45import android.widget.SimpleCursorAdapter; 46import android.widget.Toast; 47import android.widget.AdapterView.OnItemClickListener; 48import android.widget.SimpleCursorAdapter.ViewBinder; 49 50public class ScriptListActivity extends ListActivity { 51 52 public final static String TAG = ScriptListActivity.class.toString(); 53 54 private ScriptDatabase scriptdb = null; 55 private SimpleCursorAdapter adapter = null; 56 57 public void onCreate(Bundle icicle) { 58 super.onCreate(icicle); 59 setContentView(R.layout.act_scriptlist); 60 61 this.scriptdb = new ScriptDatabase(this); 62 63 final Cursor cur = scriptdb.getScripts(); 64 this.startManagingCursor(cur); 65 66 adapter = new SimpleCursorAdapter(this, R.layout.item_script, cur, 67 new String[] { ScriptDatabase.FIELD_SCRIPT_NAME, ScriptDatabase.FIELD_SCRIPT_DESCRIP, ScriptDatabase.FIELD_SCRIPT_ENABLED }, 68 new int[] { R.id.script_title, R.id.script_descrip, R.id.script_enabled }); 69 70 adapter.setViewBinder(new ViewBinder() { 71 public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 72 switch(view.getId()) { 73 case R.id.script_enabled: { 74 if(!(view instanceof CheckBox)) return false; 75 int value = cursor.getInt(columnIndex); 76 ((CheckBox)view).setChecked((value == 1)); 77 return true; 78 } 79 } 80 return false; 81 } 82 }); 83 84 this.getListView().setOnItemClickListener(new OnItemClickListener() { 85 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 86 // toggle checked status and refresh our cursor 87 scriptdb.toggleEnabled(id); 88 cur.requery(); 89 90 } 91 }); 92 93 this.setListAdapter(adapter); 94 this.registerForContextMenu(this.getListView()); 95 96 } 97 98 public void onDestroy() { 99 super.onDestroy(); 100 this.scriptdb.close(); 101 } 102 103 104 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 105 final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; 106 107 MenuItem delete = menu.add(R.string.manage_delete); 108 delete.setOnMenuItemClickListener(new OnMenuItemClickListener() { 109 public boolean onMenuItemClick(MenuItem item) { 110 scriptdb.deleteScript(info.id); 111 adapter.getCursor().requery(); 112 return true; 113 } 114 }); 115 116 } 117 118 /** 119 * File filter to show only user scripts (*.user.js) 120 */ 121 protected FilenameFilter userScriptFilter = new FilenameFilter() { 122 public boolean accept(File path, String filename) { 123 return filename.endsWith(".user.js"); 124 } 125 }; 126 127 public boolean onCreateOptionsMenu(Menu menu) { 128 super.onCreateOptionsMenu(menu); 129 130 MenuItem find = menu.add(R.string.manage_find); 131 find.setIcon(android.R.drawable.ic_menu_add); 132 find.setOnMenuItemClickListener(new OnMenuItemClickListener() { 133 public boolean onMenuItemClick(MenuItem item) { 134 // launch back around to specific url 135 Intent intent = new Intent(ScriptListActivity.this, BrowserActivity.class); 136 intent.putExtra(SearchManager.QUERY, "http://userscripts.org/scripts/search?q=android"); 137 ScriptListActivity.this.startActivity(intent); 138 return true; 139 } 140 }); 141 142 MenuItem imp = menu.add(R.string.manage_import); 143 imp.setIcon(android.R.drawable.ic_menu_upload); 144 imp.setOnMenuItemClickListener(new OnMenuItemClickListener() { 145 public boolean onMenuItemClick(MenuItem item) { 146 // show file browser to pick script 147 148 // code adapted from connectbot project: 149 // http://connectbot.googlecode.com/svn/trunk/connectbot/src/org/connectbot/PubkeyListActivity.java 150 151 // TODO: replace this with well-known intent over to file browser 152 // TODO: if browser not installed (?) then fallback to this simple method? 153 154 // build list of all files in sdcard root 155 final File sdcard = Environment.getExternalStorageDirectory(); 156 157 // Don't show a dialog if the SD card is completely absent. 158 final String state = Environment.getExternalStorageState(); 159 if (Environment.MEDIA_REMOVED.equals(state) 160 || Environment.MEDIA_BAD_REMOVAL.equals(state) 161 || Environment.MEDIA_UNMOUNTABLE.equals(state) 162 || Environment.MEDIA_UNMOUNTED.equals(state)) { 163 164 Toast.makeText(ScriptListActivity.this, R.string.manage_pick_error, Toast.LENGTH_SHORT).show(); 165 return true; 166 } 167 168 final String[] namesList = sdcard.list(userScriptFilter); 169 170 // prompt user to select any file from the sdcard root 171 new AlertDialog.Builder(ScriptListActivity.this) 172 .setTitle(R.string.manage_pick) 173 .setItems(namesList, new OnClickListener() { 174 public void onClick(DialogInterface arg0, int arg1) { 175 // find the exact file selected 176 String name = namesList[arg1]; 177 try { 178 String raw = Util.getFileString(new File(sdcard, name)); 179 scriptdb.insertScript(null, raw); 180 Toast.makeText(ScriptListActivity.this, R.string.manage_import_success, Toast.LENGTH_SHORT).show(); 181 } catch(Exception e) { 182 Log.e(TAG, "Problem while trying to import script", e); 183 Toast.makeText(ScriptListActivity.this, R.string.manage_import_fail, Toast.LENGTH_SHORT).show(); 184 } 185 } 186 }) 187 .setNegativeButton(android.R.string.cancel, null).create().show(); 188 189 return true; 190 } 191 }); 192 193 return true; 194 } 195 196}