| 1 | /* |
| 2 | * Tomdroid |
| 3 | * Tomboy on Android |
| 4 | * http://www.launchpad.net/tomdroid |
| 5 | * |
| 6 | * Copyright 2009, 2010, 2011 Olivier Bilodeau <olivier@bottomlesspit.org> |
| 7 | * Copyright 2009, Benoit Garret <benoit.garret_launchpad@gadz.org> |
| 8 | * Copyright 2010, Rodja Trappe <mail@rodja.net> |
| 9 | * |
| 10 | * This file is part of Tomdroid. |
| 11 | * |
| 12 | * Tomdroid is free software: you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation, either version 3 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * Tomdroid is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | package org.tomdroid.ui; |
| 26 | |
| 27 | import org.tomdroid.Note; |
| 28 | import org.tomdroid.NoteManager; |
| 29 | import org.tomdroid.R; |
| 30 | import org.tomdroid.sync.ServiceAuth; |
| 31 | import org.tomdroid.sync.SyncManager; |
| 32 | import org.tomdroid.sync.SyncService; |
| 33 | import org.tomdroid.util.FirstNote; |
| 34 | import org.tomdroid.util.Preferences; |
| 35 | import org.tomdroid.util.Send; |
| 36 | |
| 37 | import android.app.AlertDialog; |
| 38 | import android.app.ListActivity; |
| 39 | import android.app.ProgressDialog; |
| 40 | import android.content.Context; |
| 41 | import android.content.DialogInterface; |
| 42 | import android.content.Intent; |
| 43 | import android.content.DialogInterface.OnClickListener; |
| 44 | import android.content.pm.PackageManager.NameNotFoundException; |
| 45 | import android.database.Cursor; |
| 46 | import android.net.Uri; |
| 47 | import android.os.Bundle; |
| 48 | import android.os.Environment; |
| 49 | import android.os.Handler; |
| 50 | import android.os.Message; |
| 51 | import android.util.Log; |
| 52 | import android.view.ContextMenu; |
| 53 | import android.view.Menu; |
| 54 | import android.view.MenuInflater; |
| 55 | import android.view.MenuItem; |
| 56 | import android.view.View; |
| 57 | import android.view.ContextMenu.ContextMenuInfo; |
| 58 | import android.widget.ListAdapter; |
| 59 | import android.widget.ListView; |
| 60 | import android.widget.TextView; |
| 61 | import android.widget.AdapterView.AdapterContextMenuInfo; |
| 62 | |
| 63 | public class Tomdroid extends ListActivity { |
| 64 | |
| 65 | // Global definition for Tomdroid |
| 66 | public static final String AUTHORITY = "org.tomdroid.notes"; |
| 67 | public static final Uri CONTENT_URI = Uri |
| 68 | .parse("content://" + AUTHORITY |
| 69 | + "/notes"); |
| 70 | public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.tomdroid.note"; |
| 71 | public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.tomdroid.note"; |
| 72 | public static final String PROJECT_HOMEPAGE = "http://www.launchpad.net/tomdroid/"; |
| 73 | |
| 74 | // config parameters |
| 75 | // TODO hardcoded for now |
| 76 | public static final String NOTES_PATH = Environment.getExternalStorageDirectory() |
| 77 | + "/tomdroid/"; |
| 78 | // Logging should be disabled for release builds |
| 79 | public static final boolean LOGGING_ENABLED = false; |
| 80 | // Set this to false for release builds, the reason should be obvious |
| 81 | public static final boolean CLEAR_PREFERENCES = false; |
| 82 | |
| 83 | // Logging info |
| 84 | private static final String TAG = "Tomdroid"; |
| 85 | |
| 86 | // UI to data model glue |
| 87 | private TextView listEmptyView; |
| 88 | private ListAdapter adapter; |
| 89 | |
| 90 | // UI feedback handler |
| 91 | private Handler syncMessageHandler = new SyncMessageHandler(this); |
| 92 | |
| 93 | /** Called when the activity is created. */ |
| 94 | @Override |
| 95 | public void onCreate(Bundle savedInstanceState) { |
| 96 | super.onCreate(savedInstanceState); |
| 97 | |
| 98 | setContentView(R.layout.main); |
| 99 | Preferences.init(this, CLEAR_PREFERENCES); |
| 100 | |
| 101 | // did we already show the warning and got destroyed by android's activity killer? |
| 102 | if (Preferences.getBoolean(Preferences.Key.FIRST_RUN)) { |
| 103 | Log.i(TAG, "Tomdroid is first run."); |
| 104 | |
| 105 | // add a first explanatory note |
| 106 | NoteManager.putNote(this, FirstNote.createFirstNote()); |
| 107 | |
| 108 | // Warn that this is a "will eat your babies" release |
| 109 | new AlertDialog.Builder(this).setMessage(getString(R.string.strWelcome)).setTitle( |
| 110 | getString(R.string.titleWelcome)).setNeutralButton(getString(R.string.btnOk), new OnClickListener() { |
| 111 | public void onClick(DialogInterface dialog, int which) { |
| 112 | Preferences.putBoolean(Preferences.Key.FIRST_RUN, false); |
| 113 | dialog.dismiss(); |
| 114 | } |
| 115 | }).setIcon(R.drawable.icon).show(); |
| 116 | } |
| 117 | |
| 118 | // adapter that binds the ListView UI to the notes in the note manager |
| 119 | adapter = NoteManager.getListAdapter(this); |
| 120 | setListAdapter(adapter); |
| 121 | |
| 122 | // set the view shown when the list is empty |
| 123 | // TODO default empty-list text is butt-ugly! |
| 124 | listEmptyView = (TextView) findViewById(R.id.list_empty); |
| 125 | getListView().setEmptyView(listEmptyView); |
| 126 | |
| 127 | registerForContextMenu((ListView)findViewById(android.R.id.list)); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean onCreateOptionsMenu(Menu menu) { |
| 132 | |
| 133 | // Create the menu based on what is defined in res/menu/main.xml |
| 134 | MenuInflater inflater = getMenuInflater(); |
| 135 | inflater.inflate(R.menu.main, menu); |
| 136 | return true; |
| 137 | |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean onOptionsItemSelected(MenuItem item) { |
| 142 | switch (item.getItemId()) { |
| 143 | case R.id.menuAbout: |
| 144 | showAboutDialog(); |
| 145 | return true; |
| 146 | |
| 147 | case R.id.menuPrefs: |
| 148 | startActivity(new Intent(this, PreferencesActivity.class)); |
| 149 | return true; |
| 150 | |
| 151 | case R.id.menuSearch: |
| 152 | startSearch(null, false, null, false); |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | return super.onOptionsItemSelected(item); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onCreateContextMenu(ContextMenu menu, View v, |
| 161 | ContextMenuInfo menuInfo) { |
| 162 | MenuInflater inflater = getMenuInflater(); |
| 163 | inflater.inflate(R.menu.main_longclick, menu); |
| 164 | menu.setHeaderTitle(getString(R.string.noteOptions)); |
| 165 | super.onCreateContextMenu(menu, v, menuInfo); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean onContextItemSelected(MenuItem item) { |
| 170 | AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); |
| 171 | long noteId = info.id; |
| 172 | Uri intentUri = Uri.parse(Tomdroid.CONTENT_URI+"/"+noteId); |
| 173 | Note note = NoteManager.getNote(this, intentUri); |
| 174 | |
| 175 | switch (item.getItemId()) { |
| 176 | case R.id.menu_send: |
| 177 | (new Send(this, note)).send(); |
| 178 | break; |
| 179 | case R.id.view: |
| 180 | this.ViewNote(noteId); |
| 181 | break; |
| 182 | |
| 183 | default: |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | return super.onContextItemSelected(item); |
| 188 | } |
| 189 | |
| 190 | public void onResume() { |
| 191 | super.onResume(); |
| 192 | Intent intent = this.getIntent(); |
| 193 | |
| 194 | SyncService currentService = SyncManager.getInstance().getCurrentService(); |
| 195 | |
| 196 | if (currentService.needsAuth() && intent != null) { |
| 197 | Uri uri = intent.getData(); |
| 198 | |
| 199 | if (uri != null && uri.getScheme().equals("tomdroid")) { |
| 200 | Log.i(TAG, "Got url : " + uri.toString()); |
| 201 | |
| 202 | final ProgressDialog dialog = ProgressDialog.show(this, "", getString(R.string.prefSyncCompleteAuth), true, false); |
| 203 | |
| 204 | Handler handler = new Handler() { |
| 205 | |
| 206 | @Override |
| 207 | public void handleMessage(Message msg) { |
| 208 | dialog.dismiss(); |
| 209 | } |
| 210 | |
| 211 | }; |
| 212 | |
| 213 | ((ServiceAuth) currentService).remoteAuthComplete(uri, handler); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | SyncManager.setActivity(this); |
| 218 | SyncManager.setHandler(this.syncMessageHandler); |
| 219 | } |
| 220 | |
| 221 | private void showAboutDialog() { |
| 222 | |
| 223 | // grab version info |
| 224 | String ver; |
| 225 | try { |
| 226 | ver = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; |
| 227 | } catch (NameNotFoundException e) { |
| 228 | e.printStackTrace(); |
| 229 | ver = "Not found!"; |
| 230 | } |
| 231 | |
| 232 | // format the string |
| 233 | String aboutDialogFormat = getString(R.string.strAbout); |
| 234 | String aboutDialogStr = String.format(aboutDialogFormat, getString(R.string.app_desc), // App description |
| 235 | getString(R.string.author), // Author name |
| 236 | ver // Version |
| 237 | ); |
| 238 | |
| 239 | // build and show the dialog |
| 240 | new AlertDialog.Builder(this).setMessage(aboutDialogStr).setTitle(getString(R.string.titleAbout)) |
| 241 | .setIcon(R.drawable.icon).setNegativeButton(getString(R.string.btnProjectPage), new OnClickListener() { |
| 242 | public void onClick(DialogInterface dialog, int which) { |
| 243 | startActivity(new Intent(Intent.ACTION_VIEW, Uri |
| 244 | .parse(Tomdroid.PROJECT_HOMEPAGE))); |
| 245 | dialog.dismiss(); |
| 246 | } |
| 247 | }).setPositiveButton(getString(R.string.btnOk), new OnClickListener() { |
| 248 | public void onClick(DialogInterface dialog, int which) { |
| 249 | dialog.dismiss(); |
| 250 | } |
| 251 | }).show(); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | protected void onListItemClick(ListView l, View v, int position, long id) { |
| 256 | |
| 257 | Cursor item = (Cursor) adapter.getItem(position); |
| 258 | long noteId = item.getInt(item.getColumnIndexOrThrow(Note.ID)); |
| 259 | this.ViewNote(noteId); |
| 260 | |
| 261 | } |
| 262 | |
| 263 | public void ViewNote(long noteId) { |
| 264 | Uri intentUri = Uri.parse(Tomdroid.CONTENT_URI + "/" + noteId); |
| 265 | Intent i = new Intent(Intent.ACTION_VIEW, intentUri, this, ViewNote.class); |
| 266 | startActivity(i); |
| 267 | } |
| 268 | |
| 269 | public static void ViewList(Context View) { |
| 270 | if ( ! ( View instanceof Tomdroid ) ) |
| 271 | { |
| 272 | View.startActivity(new Intent(View, Tomdroid.class) |
| 273 | .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | } |