Android Open Source - DashNotifier Icon Picker






From Project

Back to project page DashNotifier.

License

The source code is released under:

MIT License

If you think the Android project DashNotifier 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.umang.dashnotifier;
//  w  w w.  jav a  2  s . com
import java.io.File;

import com.umang.dashnotifier.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.AdapterView.OnItemClickListener;

public class IconPicker implements Preference.OnPreferenceClickListener {
  public static final String RESOURCE_NAME = "resource_name";
  public static final String PACKAGE_NAME = "package_name";
  public static final String TAG = "IconPicker";
  public static final int REQUEST_PICK_SYSTEM = 0;
  public static final int REQUEST_PICK_GALLERY = 1;
  public static final int REQUEST_PICK_ICON_PACK = 2;
  private Activity mParent;
  //private Context mParent;
  private Resources mResources;
  private Preference mPreference;
  private OnIconPickListener mIconListener;
  String extNumber;
  SharedPreferences preferences;
  SharedPreferences.Editor editor;
  String icon;
  
  
  public static interface OnIconPickListener {
    public abstract void iconPicked();
  }
  
  public IconPicker(Activity activity, OnIconPickListener oniconpicklistener, String s) {
    mParent = activity;
    mIconListener = oniconpicklistener;
    extNumber = s;
    mResources = activity.getResources();
    preferences = PreferenceManager.getDefaultSharedPreferences(activity);
    editor = preferences.edit();
  }

  @Override
  public boolean onPreferenceClick(Preference preference) {
    mPreference = preference;
    Log.v(TAG, mParent.getCacheDir().getAbsolutePath());
    icon = preferences.getString(mPreference.getKey(), "dummy");
    pickIcon();
    return true;
  }

  public void pickIcon() {

    String[] items = new String[3];
    items[0] = mResources
        .getString(R.string.icon_picker_dn_icons_title);
    items[1] = mResources.getString(R.string.icon_picker_gallery_title);
    items[2] = mResources.getString(R.string.icon_picker_pack_title);

    new AlertDialog.Builder(mParent, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
        .setTitle(R.string.icon_picker_title)
        .setItems(items, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int item) {
            showChosen(item);
          }
        }).show();
  }

  private void showChosen(final int type) {
    if (type == REQUEST_PICK_SYSTEM) {
      
      
      final IconAdapter adapter = new IconAdapter();

      final Dialog dialog = new Dialog(mParent,
          android.R.style.Theme_DeviceDefault_Dialog);
      

      dialog.setTitle(R.string.icon_picker_choose_icon_title);
      dialog.setContentView(R.layout.icon_picker_grid);
      GridView gridview = (GridView) dialog.findViewById(R.id.gridview);
        gridview.setAdapter(adapter);

      
        gridview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          Intent intent = new Intent();
          intent.putExtra(RESOURCE_NAME,
              adapter.getItemReference(position));
          System.out.println(adapter.getItemReference(position));
          editor.putString(mPreference.getKey(),
              adapter.getItemReference(position));
          editor.putString("icon_preference_default_"+extNumber, adapter.getItemReference(position));
          editor.remove((new StringBuilder("iconExt")).append(extNumber).toString());
          editor.commit();
          mIconListener.iconPicked();
          dialog.dismiss();
        }
      });
      dialog.show();
    } else if (type == REQUEST_PICK_GALLERY) {
      File image = new File(mParent.getFilesDir() + "/icon_"+extNumber + ".png");
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
            try {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
                mParent.startActivityForResult(intent, type);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
            }
      
    } else if (type == REQUEST_PICK_ICON_PACK) {
      try {
        Intent intent = new Intent("org.adw.launcher.icons.ACTION_PICK_ICON");
        mParent.startActivityForResult(intent,type);
      } catch (ActivityNotFoundException e) {
        Toast.makeText(mParent, R.string.no_icon_packs, Toast.LENGTH_LONG).show();
      }
    }
     
  }

  class IconAdapter extends BaseAdapter {
    String[] labels;
    TypedArray icons;
    int bound;
    int padding;

    public IconAdapter() {
      
      icons = mResources.obtainTypedArray(R.array.icons);
      bound = mParent.getResources().getDimensionPixelSize(
          R.dimen.grid_icon_bound);
      padding = mParent.getResources().getDimensionPixelSize(
          R.dimen.grid_icon_padding);

    }

    @Override
    public int getCount() {
      return icons.length();
    }

    @Override
    public Object getItem(int position) {
      return icons.getDrawable(position);
    }

    public String getItemReference(int position) {
      String name = icons.getString(position);
      int separatorIndex = name.lastIndexOf(File.separator);
      int periodIndex = name.lastIndexOf('.');
      return name.substring(separatorIndex + 1, periodIndex);
    }

    @Override
    public long getItemId(int position) {
      return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mParent);
                imageView.setLayoutParams(new GridView.LayoutParams(bound, bound));
                imageView.setPadding(padding, padding, padding, padding);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageDrawable(((Drawable) getItem(position)).mutate());
            return imageView;
        }
    
    public int getItemPosition(String iconName) {
      for (int i = 0; i < icons.length(); i++) {
        if (getItemReference(i).equals(iconName))
          return i;
      }
      return -1;
    }
  }

  
}




Java Source Code List

com.google.android.apps.dashclock.configuration.AppChooserPreference.java
com.google.android.apps.dashclock.ui.SimplePagedTabsHelper.java
com.umang.dashnotifier.AbstractSettings.java
com.umang.dashnotifier.AppListFragment.java
com.umang.dashnotifier.AppSelectActivity.java
com.umang.dashnotifier.ClickIntentActivity.java
com.umang.dashnotifier.Commons.java
com.umang.dashnotifier.DashNotificationListenerAcc.java
com.umang.dashnotifier.DashNotificationListener.java
com.umang.dashnotifier.DashNotifierExtension10.java
com.umang.dashnotifier.DashNotifierExtension11.java
com.umang.dashnotifier.DashNotifierExtension2.java
com.umang.dashnotifier.DashNotifierExtension3.java
com.umang.dashnotifier.DashNotifierExtension4.java
com.umang.dashnotifier.DashNotifierExtension5.java
com.umang.dashnotifier.DashNotifierExtension6.java
com.umang.dashnotifier.DashNotifierExtension7.java
com.umang.dashnotifier.DashNotifierExtension8.java
com.umang.dashnotifier.DashNotifierExtension9.java
com.umang.dashnotifier.DashNotifierExtension.java
com.umang.dashnotifier.DashNotifierSettingsActivity10.java
com.umang.dashnotifier.DashNotifierSettingsActivity11.java
com.umang.dashnotifier.DashNotifierSettingsActivity2.java
com.umang.dashnotifier.DashNotifierSettingsActivity3.java
com.umang.dashnotifier.DashNotifierSettingsActivity4.java
com.umang.dashnotifier.DashNotifierSettingsActivity5.java
com.umang.dashnotifier.DashNotifierSettingsActivity6.java
com.umang.dashnotifier.DashNotifierSettingsActivity7.java
com.umang.dashnotifier.DashNotifierSettingsActivity8.java
com.umang.dashnotifier.DashNotifierSettingsActivity9.java
com.umang.dashnotifier.DashNotifierSettingsActivity.java
com.umang.dashnotifier.HelpUtils.java
com.umang.dashnotifier.IconPicker.java
com.umang.dashnotifier.PackageAdapter.java
com.umang.dashnotifier.PackageItem.java
com.umang.dashnotifier.PrefsFragment.java
com.umang.dashnotifier.UnlockReceiver.java
com.umang.dashnotifier.provider.NotifSQLiteHelper.java
com.umang.dashnotifier.provider.NotificationProvider.java
com.umang.dashnotifier.provider.NotificationStore.java