Android Open Source - my-wallpaper Category Activity






From Project

Back to project page my-wallpaper.

License

The source code is released under:

MIT License

If you think the Android project my-wallpaper 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

package com.koonen.photostream;
// www  . j  a  v  a 2s. c  o m
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.koonen.photostream.dao.Category;
import com.koonen.utils.DialogUtils;
import com.koonen.utils.DialogUtils.ClickHandler;

/**
 * 
 * @author Glick
 * 
 */
public class CategoryActivity extends ListActivity {

  private static final String TAG = CategoryActivity.class.getCanonicalName();

  private static final int MENU_ITEM_CATEGORY_ADD = 1;
  private static final int MENU_ITEM_CATEGORY_UPDATE = 2;
  private static final int MENU_ITEM_CATEGORY_DELETE = 3;

  private CategoryAdapter adapter;

  private static final String PREFERENCES_CATEGORY = "CATEGORY";
  private static final String PREFERENCE_CATEGORY_IS_FIRST = "eula.isFirst";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    adapter = new CategoryAdapter(this);
    this.setListAdapter(adapter);

    final SharedPreferences preferences = getSharedPreferences(
        PREFERENCES_CATEGORY, Activity.MODE_PRIVATE);
    if (!preferences.getBoolean(PREFERENCE_CATEGORY_IS_FIRST, false)) {

      DialogUtils.ClickHandler clickHandler = new ClickHandler() {

        @Override
        public void handle() {
          commitCategoryIsFirst(preferences);
        }

      };

      DialogUtils.showInfoDialog(this, R.string.category_first_open,
          clickHandler, clickHandler);
    }
  }

  private void commitCategoryIsFirst(SharedPreferences preferences) {
    preferences.edit().putBoolean(PREFERENCE_CATEGORY_IS_FIRST, true)
        .commit();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menu.add(0, MENU_ITEM_CATEGORY_ADD, 0, R.string.menu_category_add)
        .setIcon(android.R.drawable.ic_menu_edit);

    menu
        .add(0, MENU_ITEM_CATEGORY_UPDATE, 1,
            R.string.menu_category_update).setIcon(
            android.R.drawable.ic_menu_edit);

    menu
        .add(0, MENU_ITEM_CATEGORY_DELETE, 2,
            R.string.menu_category_delete).setIcon(
            android.R.drawable.ic_menu_delete);

    return true;
  }

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    int position = getSelectedItemPosition();
    if (position != AdapterView.INVALID_POSITION) {
      Category category = (Category) adapter.getItem(position);
      boolean canModify = adapter.isExistCategory(category)
          && !adapter.isUnmodifiable(category);
      menu.findItem(MENU_ITEM_CATEGORY_UPDATE).setEnabled(canModify);
      menu.findItem(MENU_ITEM_CATEGORY_DELETE).setEnabled(canModify);
    } else {
      menu.findItem(MENU_ITEM_CATEGORY_UPDATE).setEnabled(false);
      menu.findItem(MENU_ITEM_CATEGORY_DELETE).setEnabled(false);
    }
    return super.onPrepareOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_ITEM_CATEGORY_ADD:
      CategoryEditActivity.show(this);
      return true;
    case MENU_ITEM_CATEGORY_UPDATE: {
      int position = getSelectedItemPosition();
      if (position != AdapterView.INVALID_POSITION) {
        Category category = (Category) adapter.getItem(position);
        CategoryEditActivity.show(this, category);
      }
      return true;
    }
    case MENU_ITEM_CATEGORY_DELETE: {
      int position = getSelectedItemPosition();
      if (position != AdapterView.INVALID_POSITION) {
        Category category = (Category) adapter.getItem(position);
        adapter.remove(category);
      }
      return true;
    }
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ActivityConstants.REQUEST_ID_CATEGORY_EDIT) {
      if (resultCode == RESULT_OK) {
        final Bundle extras = data.getExtras();

        Category category = null;
        boolean isEdit = false;
        boolean success = true;
        if (extras != null) {
          category = extras
              .getParcelable(CategoryEditActivity.EXTRA_CATEGORY);
          isEdit = extras
              .getBoolean(CategoryEditActivity.EXTRA_EDIT_CATEGORY);
        }
        if (category == null) {
          success = false;
        } else {
          try {
            Category category2 = adapter.getCategoryByName(category
                .getName());
            if (isEdit) {
              if (category2 == null
                  || category2.getId() == category.getId()) {
                adapter.update(category);
                showMessage(R.string.category_save_successfully);
              } else {
                showMessage(R.string.category_exist);
              }
            } else {
              if (category2 == null) {
                adapter.add(category);
                showMessage(R.string.category_save_successfully);
              } else {
                showMessage(R.string.category_exist);
              }
            }
          } catch (Exception e) {
            Log.e(TAG, "Couldn't save category", e);
            success = false;
          }
        }
        if (!success) {
          showMessage(R.string.category_save_fail);
        }
      }
    }
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (position != AdapterView.INVALID_POSITION) {
      Category category = (Category) adapter.getItem(position);
      Intent intent = new Intent();
      intent.putExtra(CategoryEditActivity.EXTRA_CATEGORY, category);
      setResult(ActivityConstants.RESULT_CATEGORY_SEARCH, intent);
      finish();
    }
  }

  private void showMessage(int resId) {
    Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
  }

  static void show(Activity context) {
    Intent intent = new Intent(context, CategoryActivity.class);
    context.startActivityForResult(intent,
        ActivityConstants.REQUEST_ID_CATEGORY_SEARCH);
  }
}




Java Source Code List

com.koonen.photostream.ActivityConstants.java
com.koonen.photostream.BootReceiver.java
com.koonen.photostream.CameraPreviewActivity.java
com.koonen.photostream.CategoryActivity.java
com.koonen.photostream.CategoryAdapter.java
com.koonen.photostream.CategoryEditActivity.java
com.koonen.photostream.CropWallpaperTask.java
com.koonen.photostream.Eula.java
com.koonen.photostream.FastBitmapDrawable.java
com.koonen.photostream.FileBrowserActivity.java
com.koonen.photostream.GridLayout.java
com.koonen.photostream.ImageUtilities.java
com.koonen.photostream.PhotostreamActivity.java
com.koonen.photostream.RotationService.java
com.koonen.photostream.ServiceConnector.java
com.koonen.photostream.SetWallpaperTask.java
com.koonen.photostream.UserTask.java
com.koonen.photostream.ViewPhotoActivity.java
com.koonen.photostream.WallPaperExecutor.java
com.koonen.photostream.api.FilePhoto.java
com.koonen.photostream.api.IPhotoService.java
com.koonen.photostream.api.Location.java
com.koonen.photostream.api.PhotoList.java
com.koonen.photostream.api.PhotoSize.java
com.koonen.photostream.api.Photo.java
com.koonen.photostream.api.ResponseHandler.java
com.koonen.photostream.api.ResponseParser.java
com.koonen.photostream.api.ServiceContext.java
com.koonen.photostream.api.ServiceException.java
com.koonen.photostream.api.ServiceManager.java
com.koonen.photostream.api.ServiceNetworkException.java
com.koonen.photostream.api.SourceType.java
com.koonen.photostream.api.Type.java
com.koonen.photostream.api.UserInfo.java
com.koonen.photostream.api.UserNotFoundException.java
com.koonen.photostream.api.User.java
com.koonen.photostream.api.flickr.Auth.java
com.koonen.photostream.api.flickr.FlickrConstants.java
com.koonen.photostream.api.flickr.FlickrService.java
com.koonen.photostream.api.flickr.Perms.java
com.koonen.photostream.dao.CategoryDAO.java
com.koonen.photostream.dao.CategoryList.java
com.koonen.photostream.dao.Category.java
com.koonen.photostream.dao.ImageDAO.java
com.koonen.photostream.dao.PhotoDAO.java
com.koonen.photostream.dao.PhotoUrlListProvider.java
com.koonen.photostream.dao.PhotoUrlList.java
com.koonen.photostream.dao.PhotoUrl.java
com.koonen.photostream.effects.EffectsApplier.java
com.koonen.photostream.effects.EffectsFactory.java
com.koonen.photostream.effects.Rotate3dAnimation.java
com.koonen.photostream.effects.TypeEffect.java
com.koonen.photostream.settings.BackgroundSource.java
com.koonen.photostream.settings.Network.java
com.koonen.photostream.settings.UserPreferences.java
com.koonen.photostream.settings.UserSettingsActivity.java
com.koonen.photostream.settings.WallpaperSettingMode.java
com.koonen.utils.ConfigurationReader.java
com.koonen.utils.DialogUtils.java
com.koonen.utils.Enumeration.java
com.koonen.utils.GroupUtils.java
com.koonen.utils.MailSender.java
com.koonen.utils.StatisticUtils.java
com.koonen.utils.StreamUtils.java