Android Open Source - checkit Home Activity






From Project

Back to project page checkit.

License

The source code is released under:

GNU General Public License

If you think the Android project checkit listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * List activity for CheckIt./*from www  .  j av  a  2  s. c o  m*/
 *
 * @author  Yujian Zhang <yujian{dot}zhang[at]gmail(dot)com>
 *
 * License: 
 *   GNU General Public License v2
 *   http://www.gnu.org/licenses/gpl-2.0.html
 * Copyright (C) 2011-2012 Yujian Zhang
 */

package net.whily.android.checkit;

import java.io.*;
import java.util.*;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public final class HomeActivity extends ListActivity 
  implements OnDialogDoneListener {
  private static final String TAG = "HomeActivity";
  private static final String sdPrefix = "checkit";

  private static final String backupPrefix = "checkit-";
  private static final String backupSuffix = ".db";

  private static final String[] PROJECTION = new String[] {
    ChecklistMetadata.Checklists._ID,
    ChecklistMetadata.Checklists.COLUMN_TITLE
  };

  private ExternalStorage sd;
  private ListView list;

  private Cursor cursor;

  private Uri selectedUri;

  private HashSet<Long> selectedIds;

  private String[] selections; // Strings for selection diaglog.

  public static final String EDIT_TITLE_DIALOG_TAG = "EDIT_TITLE_DIALOG";
  public static final String NEW_TITLE_DIALOG_TAG = "NEW_TITLE_DIALG";
  public static final String FROM_TEMPLATE_DIALOG_TAG = "FROM_TEMPLATE_DIALOG";
  public static final String RESTORE_DIALOG_TAG = "RESTORE_DIALOG";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    if (intent.getData() == null) { // Launched by user directly.
      intent.setData(ChecklistMetadata.Checklists.CONTENT_URI);
    }

    sd = new ExternalStorage(sdPrefix, this);
    selectedIds = new HashSet<Long>();
    if (savedInstanceState != null) {
      long[] array = savedInstanceState.getLongArray("selectedIds");
      for (long selectedId : array) {
        selectedIds.add(selectedId);
      }
    } else {
      selectedIds = new HashSet<Long>();
    }

    setContentView(R.layout.home);
    ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(true);

    cursor = managedQuery(getIntent().getData(),
                          PROJECTION, null, null, 
                          ChecklistMetadata.Checklists.DEFAULT_SORT_ORDER);
    String[] dataColumns = { ChecklistMetadata.Checklists.COLUMN_TITLE };
    int[] viewIDs = { android.R.id.text1 };
    HighlightCursorAdapter adapter
      = new HighlightCursorAdapter(this,
                                   android.R.layout.simple_list_item_1,
                                   cursor,
                                   dataColumns,
                                   viewIDs
                                   );
    setListAdapter(adapter);
    list = (ListView)getListView();
    list.setMultiChoiceModeListener(new ModeCallback());
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    long[] array = new long[selectedIds.size()];
    int i = 0;
    for (long selectedId : selectedIds) {
      array[i++] = selectedId;
    }
    outState.putLongArray("selectedIds", array);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.home_options, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
      case android.R.id.home:
      case R.id.about:
        startActivity(new Intent(this, AboutActivity.class));
        return true;

      case R.id.backup:
        backup();
        return true;

      case R.id.new_list:
        newChecklist();
        return true;

      case R.id.import_list:
        importChecklist();
        return true;

      case R.id.template_list:
        createFromTemplate();
        return true;

      case R.id.restore:
        restore();
        return true;

      case R.id.settings:
        startActivity(new Intent(this, SettingsActivity.class));
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }
    
  //@Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
    startActivity(new Intent(Intent.ACTION_EDIT, uri));
  }

  private void editChecklistTitle() {
    String title = getSelectedChecklistTitle();
    // Save the URI since selectedIds will be cleard when the dialog is shown.
    selectedUri = getSelectedUri(); 

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    PromptDialogFragment pdf 
      = PromptDialogFragment.newInstance(R.string.edit_title, title);
    pdf.show(ft, EDIT_TITLE_DIALOG_TAG);
  }

  private void newChecklist() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    PromptDialogFragment pdf 
      = PromptDialogFragment.newInstance(R.string.edit_title, 
                                         getString(R.string.title));
    pdf.show(ft, NEW_TITLE_DIALOG_TAG);    
  }

  private void importChecklist() {
  }

  private void backup() {
    String backupName = backupPrefix + Util.timeStamp() + backupSuffix;
    try {
      sd.copyToSD(ChecklistMetadata.DATABASE_NAME, backupName);
      String filePath = sd.getSDFile(backupName).toString();
      Util.toast(this, 
                 getString(R.string.backup_successful) + " " + filePath + ".");
    } catch (Exception e) {
      // Do nothing since exception is already handled. The main intention here
      // is to avoid showing the "successful" message.
    }
  }

  private void restore() {
    if (ExternalStorage.getSDState() == ExternalStorage.SDState.UNAVAILABLE) {
      Util.toast(this, getString(R.string.sd_not_mounted));
      return;
    } 

    File dir = sd.getSDDir();
    if (!dir.exists()) {
      Util.toast(this, getString(R.string.no_backup_dir));
      return;
    }

    String[] files = dir.list(new FilenameFilter() {
        public boolean accept(File dir, String name) {
          return name.matches(backupPrefix + "\\d{12}" + backupSuffix);
        }
      });
    if (files.length == 0) {
      Util.toast(this, getString(R.string.no_backup_files));
      return;
    }
    Arrays.sort(files, Collections.reverseOrder());
    // We only show the latest 9 backups.
    selections = Arrays.copyOfRange(files, 0, Math.min(9, files.length));
    
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    SelectionDialogFragment sdf 
      = SelectionDialogFragment.newInstance(R.string.select_template, 
                                            selections);
    sdf.show(ft, RESTORE_DIALOG_TAG);
  }

  private void createFromTemplate() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    selections = getResources().getStringArray(R.array.template_list);
    SelectionDialogFragment sdf 
      = SelectionDialogFragment.newInstance(R.string.select_template, selections);
    sdf.show(ft, FROM_TEMPLATE_DIALOG_TAG);
  }

  private String getSelectedChecklistTitle() {
    Uri uri = getSelectedUri();
    Cursor c = managedQuery(uri, null, null, null, null);
    int titleIndex = c.getColumnIndex(ChecklistMetadata.Checklists.COLUMN_TITLE);
    c.moveToFirst();
    return c.getString(titleIndex);
  }

  // Return the id of the first selected checklist.
  private long getSelectedId() {
    return ((Long)(selectedIds.toArray()[0])).longValue();
  }

  // Return the uri of the first selected checklist.
  private Uri getSelectedUri() {
    return ContentUris.withAppendedId(getIntent().getData(), getSelectedId());
  }

  private void insertEdit(ContentValues cv) {
    Uri uri = getContentResolver().insert(getIntent().getData(), cv);
    if (uri == null) {
      Log.e(TAG, "Failed to insert new checklist into " + getIntent().getData());
      finish();     // Close activity.
      return;
    }
    startActivity(new Intent(Intent.ACTION_EDIT, uri));
  }

  public void onDialogDone(String tag, boolean cancelled, CharSequence message) {
    if (!cancelled) {
      if (tag.equals(EDIT_TITLE_DIALOG_TAG) || tag.equals(NEW_TITLE_DIALOG_TAG)) {
        String title = message.toString().trim();
        ContentValues cv = new ContentValues();
        cv.put(ChecklistMetadata.Checklists.COLUMN_TITLE, title);
        if (tag.equals(EDIT_TITLE_DIALOG_TAG)) {
          getContentResolver().update(selectedUri, cv, null, null);
        } else if (tag.equals(NEW_TITLE_DIALOG_TAG)) {
          insertEdit(cv);
        }
      } else if (tag.equals(FROM_TEMPLATE_DIALOG_TAG)) {
        String title = selections[Integer.valueOf(message.toString())];
        ContentValues cv = new ContentValues();

        String[] itemStrings = getResources().getStringArray(R.array.travel_list);
        ArrayList<CheckedItem> items = new ArrayList<CheckedItem>();
        for (String itemString : itemStrings) {
          items.add(new CheckedItem(itemString));
        }
        String content = CheckedItem.serialize(items);

        cv.put(ChecklistMetadata.Checklists.COLUMN_TITLE, title);
        cv.put(ChecklistMetadata.Checklists.COLUMN_CONTENT, content);
        insertEdit(cv);
      } else if (tag.equals(RESTORE_DIALOG_TAG)) {
        final String file = selections[Integer.valueOf(message.toString())];
        final HomeActivity outer = this;

        Alert.showTitleIcon(this, android.R.drawable.ic_dialog_alert,
                            R.string.restore, 
                            R.string.restore_checklists, 
                            R.string.restore, R.string.cancel,
                            new DialogInterface.OnClickListener () {
                              public void onClick(DialogInterface dialog, int id) {
                                sd.copyFromSD(file, ChecklistMetadata.DATABASE_NAME);
                                // Refresh the list view.
                                cursor.requery();
                                Util.toast(outer, getString(R.string.restore_successful));
                              }
                            });
      }
    }
  }

  private void deleteChecklists() {
    final String selectedIdString = selectedIds.toString();
    final String where = ChecklistMetadata.Checklists._ID
      + " IN (" + selectedIdString.substring(1, selectedIdString.length() - 1) 
      + ")";
    Alert.showTitleIcon(this, android.R.drawable.ic_dialog_alert,
                        R.string.delete, 
                        R.string.delete_checklists, 
                        R.string.delete, R.string.cancel,
                        new DialogInterface.OnClickListener () {
                          public void onClick(DialogInterface dialog, int id) {
                            getContentResolver().delete(getIntent().getData(), where, null);
                            selectedIds.clear();
                          }
                        });
  }

  private void exportChecklists() {
  }

  private final class ModeCallback implements ListView.MultiChoiceModeListener {
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
      getMenuInflater().inflate(R.menu.home_context, menu);
      setTitle(mode);
      setMenuItemVisibility(menu);
      return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
      return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
      switch (item.getItemId()) {
        case R.id.edit_title:
          editChecklistTitle();
          mode.finish();
          break;
        case R.id.delete:
          deleteChecklists();
          mode.finish();
          break;
        case R.id.export:
          exportChecklists();
          mode.finish();
        default:
          break;
      }
      return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
      selectedIds.clear();
    }

    public void onItemCheckedStateChanged(ActionMode mode,
                                          int position, long id, boolean checked) {
      if (checked) {
        selectedIds.add(id);
      } else {
        selectedIds.remove(id);
      }
      setMenuItemVisibility(mode.getMenu());
      setTitle(mode);
      getListView().invalidateViews();
    }

    private final void setTitle(ActionMode mode) {
      final int selectedCount = selectedIds.size();
      String title = 
        (selectedCount == 0) 
        ? "" 
        : getString(R.string.item_selected_prefix) + selectedCount + getString(R.string.item_selected_postfix);
      mode.setTitle(title);
    }

    private final void setMenuItemVisibility(Menu menu) {
      final int selectedCount = selectedIds.size();
      MenuItem editMenuItem = (MenuItem)menu.findItem(R.id.edit_title);
      editMenuItem.setVisible(selectedCount == 1);
    }
  }

  class HighlightCursorAdapter extends SimpleCursorAdapter {
    HighlightCursorAdapter(Context context, int layout, Cursor c, 
                           String[] from, int[] to) {
      super(context, layout, c, from, to);
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
      View view = super.getView(position, convertView, parent);
      long id = getItemId(position);
      if (selectedIds.contains(id)) {
        view.setBackgroundResource(R.drawable.light_blue);
      } else {
        view.setBackgroundResource(0);
      }
      return view;
    }
  }
}




Java Source Code List

net.whily.android.checkit.AboutActivity.java
net.whily.android.checkit.Alert.java
net.whily.android.checkit.CheckActivity.java
net.whily.android.checkit.CheckedItem.java
net.whily.android.checkit.ChecklistMetadata.java
net.whily.android.checkit.ChecklistProvider.java
net.whily.android.checkit.CustomDialogFragment.java
net.whily.android.checkit.ExternalStorage.java
net.whily.android.checkit.HomeActivity.java
net.whily.android.checkit.OnDialogDoneListener.java
net.whily.android.checkit.PromptDialogFragment.java
net.whily.android.checkit.SelectionDialogFragment.java
net.whily.android.checkit.SettingsActivity.java
net.whily.android.checkit.Util.java