diff options
Diffstat (limited to 'src/us/replicant/ReplicantWallpapers/WallpaperChooser.java')
-rw-r--r-- | src/us/replicant/ReplicantWallpapers/WallpaperChooser.java | 281 |
1 files changed, 281 insertions, 0 deletions
diff --git a/src/us/replicant/ReplicantWallpapers/WallpaperChooser.java b/src/us/replicant/ReplicantWallpapers/WallpaperChooser.java new file mode 100644 index 0000000..137c59c --- /dev/null +++ b/src/us/replicant/ReplicantWallpapers/WallpaperChooser.java @@ -0,0 +1,281 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Modified by Paul Kocialkowski to add about button. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package us.replicant.ReplicantWallpapers; + +import android.app.Activity; +import android.content.res.Resources; +import android.graphics.BitmapFactory; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.os.AsyncTask; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.View.OnClickListener; +import android.widget.AdapterView; +import android.widget.BaseAdapter; +import android.widget.Gallery; +import android.widget.ImageView; +import android.app.AlertDialog; +import android.content.Context; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.lang.String; + +public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener, + OnClickListener { + private static final String TAG = "ReplicantWallpapers"; + + private Gallery mGallery; + private ImageView mImageView; + private boolean mIsWallpaperSet; + + private Bitmap mBitmap; + + private ArrayList<Integer> mThumbs; + private ArrayList<Integer> mImages; + private String[] mAuthors = null; + private String[] mLicenses = null; + + private WallpaperLoader mLoader; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + requestWindowFeature(Window.FEATURE_NO_TITLE); + + findWallpapers(); + + setContentView(R.layout.wallpaper_chooser); + + mGallery = (Gallery) findViewById(R.id.gallery); + mGallery.setAdapter(new ImageAdapter(this)); + mGallery.setOnItemSelectedListener(this); + mGallery.setCallbackDuringFling(false); + + findViewById(R.id.set).setOnClickListener(this); + + mImageView = (ImageView) findViewById(R.id.wallpaper); + } + + private void findWallpapers() { + final Resources resources = getResources(); + final String packageName = getApplication().getPackageName(); + + int mImagesSize=resources.getStringArray(R.array.wallpapers).length; + int mAuthorsSize=resources.getStringArray(R.array.authors).length; + int mLicensesSize=resources.getStringArray(R.array.licenses).length; + + mThumbs = new ArrayList<Integer>(mImagesSize); + mImages = new ArrayList<Integer>(mImagesSize); + mAuthors = new String[mAuthorsSize]; + mLicenses = new String[mLicensesSize]; + + addWallpapers(resources, packageName, R.array.wallpapers, R.array.authors, R.array.licenses); + addWallpapers(resources, packageName, R.array.extra_wallpapers, R.array.extra_authors, R.array.extra_licenses); + } + + private void addWallpapers(Resources resources, String packageName, int list_w, int list_a, int list_l) { + final String[] extras_w = resources.getStringArray(list_w); + final String[] extras_a = resources.getStringArray(list_a); + final String[] extras_l = resources.getStringArray(list_l); + int i=0, j=0; + + for (String extra : extras_w) { + int res = resources.getIdentifier(extra, "drawable", packageName); + if (res != 0) { + final int thumbRes = resources.getIdentifier(extra + "_small", + "drawable", packageName); + + if (thumbRes != 0) { + mThumbs.add(thumbRes); + mImages.add(res); + mAuthors[j]=extras_a[i]; + mLicenses[j]=extras_l[i]; + j++; + // Log.d(TAG, "addWallpapers: [" + packageName + "]: " + extra + " (" + res + ")"); + } + } + i++; + } + } + + @Override + protected void onResume() { + super.onResume(); + mIsWallpaperSet = false; + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) { + mLoader.cancel(true); + mLoader = null; + } + } + + public void onItemSelected(AdapterView parent, View v, int position, long id) { + if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) { + mLoader.cancel(); + } + mLoader = (WallpaperLoader) new WallpaperLoader().execute(position); + } + + /* + * When using touch if you tap an image it triggers both the onItemClick and + * the onTouchEvent causing the wallpaper to be set twice. Ensure we only + * set the wallpaper once. + */ + private void selectWallpaper(int position) { + if (mIsWallpaperSet) { + return; + } + + mIsWallpaperSet = true; + try { + InputStream stream = getResources().openRawResource(mImages.get(position)); + setWallpaper(stream); + setResult(RESULT_OK); + finish(); + } catch (IOException e) { + Log.e("Paperless System", "Failed to set wallpaper: " + e); + } + } + + public void onNothingSelected(AdapterView parent) { + } + + private class ImageAdapter extends BaseAdapter { + private LayoutInflater mLayoutInflater; + + ImageAdapter(WallpaperChooser context) { + mLayoutInflater = context.getLayoutInflater(); + } + + public int getCount() { + return mThumbs.size(); + } + + public Object getItem(int position) { + return position; + } + + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + ImageView image; + + if (convertView == null) { + image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false); + } else { + image = (ImageView) convertView; + } + + int thumbRes = mThumbs.get(position); + image.setImageResource(thumbRes); + Drawable thumbDrawable = image.getDrawable(); + if (thumbDrawable != null) { + thumbDrawable.setDither(true); + } else { + Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #" + + position); + } + return image; + } + } + + public void onClick(View v) { + selectWallpaper(mGallery.getSelectedItemPosition()); + } + + class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> { + BitmapFactory.Options mOptions; + + WallpaperLoader() { + mOptions = new BitmapFactory.Options(); + mOptions.inDither = false; + mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; + } + + protected Bitmap doInBackground(Integer... params) { + if (isCancelled()) return null; + try { + return BitmapFactory.decodeResource(getResources(), + mImages.get(params[0]), mOptions); + } catch (OutOfMemoryError e) { + return null; + } + } + + @Override + protected void onPostExecute(Bitmap b) { + if (b == null) return; + + if (!isCancelled() && !mOptions.mCancel) { + // Help the GC + if (mBitmap != null) { + mBitmap.recycle(); + } + + final ImageView view = mImageView; + view.setImageBitmap(b); + + mBitmap = b; + + final Drawable drawable = view.getDrawable(); + drawable.setFilterBitmap(true); + drawable.setDither(true); + + view.postInvalidate(); + + mLoader = null; + } else { + b.recycle(); + } + } + + void cancel() { + mOptions.requestCancelDecode(); + super.cancel(true); + } + + } + + public void onAbout(View view) { + int position = mGallery.getSelectedItemPosition(); + AlertDialog.Builder about_dialog_builder=new AlertDialog.Builder(this); + + String about_dialog_message = getString(R.string.author) + ' ' + mAuthors[position] + '\n' + + getString(R.string.license) + ' ' + mLicenses[position]; + + about_dialog_builder.setMessage(about_dialog_message); + about_dialog_builder.setPositiveButton(android.R.string.ok, null); + about_dialog_builder.show(); + + } +} |