FilterShowActivity.java revision 588c30c74178bc399b043a5401f0c1f93ae22378
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.gallery3d.filtershow; 18 19import android.annotation.TargetApi; 20import android.app.ActionBar; 21import android.app.Activity; 22import android.app.ProgressDialog; 23import android.app.WallpaperManager; 24import android.content.ContentValues; 25import android.content.Context; 26import android.content.Intent; 27import android.content.res.Configuration; 28import android.content.res.Resources; 29import android.graphics.Bitmap; 30import android.graphics.BitmapFactory; 31import android.graphics.Color; 32import android.graphics.Point; 33import android.graphics.drawable.BitmapDrawable; 34import android.graphics.drawable.Drawable; 35import android.net.Uri; 36import android.os.AsyncTask; 37import android.os.Bundle; 38import android.provider.MediaStore; 39import android.util.DisplayMetrics; 40import android.util.Log; 41import android.util.TypedValue; 42import android.view.Display; 43import android.view.LayoutInflater; 44import android.view.Menu; 45import android.view.MenuItem; 46import android.view.View; 47import android.view.View.OnClickListener; 48import android.view.WindowManager; 49import android.widget.AdapterView; 50import android.widget.AdapterView.OnItemClickListener; 51import android.widget.FrameLayout; 52import android.widget.ImageButton; 53import android.widget.LinearLayout; 54import android.widget.ListView; 55import android.widget.SeekBar; 56import android.widget.ShareActionProvider; 57import android.widget.ShareActionProvider.OnShareTargetSelectedListener; 58import android.widget.Toast; 59 60import com.android.gallery3d.R; 61import com.android.gallery3d.data.LocalAlbum; 62import com.android.gallery3d.filtershow.cache.FilteringPipeline; 63import com.android.gallery3d.filtershow.cache.ImageLoader; 64import com.android.gallery3d.filtershow.editors.BasicEditor; 65import com.android.gallery3d.filtershow.editors.EditorDraw; 66import com.android.gallery3d.filtershow.editors.EditorManager; 67import com.android.gallery3d.filtershow.filters.*; 68import com.android.gallery3d.filtershow.imageshow.ImageCrop; 69import com.android.gallery3d.filtershow.imageshow.ImageDraw; 70import com.android.gallery3d.filtershow.imageshow.ImageFlip; 71import com.android.gallery3d.filtershow.imageshow.ImageRedEyes; 72import com.android.gallery3d.filtershow.imageshow.ImageRotate; 73import com.android.gallery3d.filtershow.imageshow.ImageShow; 74import com.android.gallery3d.filtershow.imageshow.ImageStraighten; 75import com.android.gallery3d.filtershow.imageshow.ImageTinyPlanet; 76import com.android.gallery3d.filtershow.imageshow.ImageZoom; 77import com.android.gallery3d.filtershow.imageshow.MasterImage; 78import com.android.gallery3d.filtershow.presets.ImagePreset; 79import com.android.gallery3d.filtershow.provider.SharedImageProvider; 80import com.android.gallery3d.filtershow.tools.SaveCopyTask; 81import com.android.gallery3d.filtershow.ui.FilterIconButton; 82import com.android.gallery3d.filtershow.ui.FramedTextButton; 83import com.android.gallery3d.filtershow.ui.Spline; 84import com.android.gallery3d.util.GalleryUtils; 85 86import java.io.File; 87import java.io.IOException; 88import java.lang.ref.WeakReference; 89import java.util.Vector; 90 91@TargetApi(16) 92public class FilterShowActivity extends Activity implements OnItemClickListener, 93 OnShareTargetSelectedListener { 94 95 // fields for supporting crop action 96 public static final String CROP_ACTION = "com.android.camera.action.CROP"; 97 private CropExtras mCropExtras = null; 98 MasterImage mMasterImage = null; 99 100 public static final String TINY_PLANET_ACTION = "com.android.camera.action.TINY_PLANET"; 101 public static final String LAUNCH_FULLSCREEN = "launch-fullscreen"; 102 public static final int MAX_BMAP_IN_INTENT = 990000; 103 private final PanelController mPanelController = new PanelController(); 104 private ImageLoader mImageLoader = null; 105 private ImageShow mImageShow = null; 106 private ImageRedEyes mImageRedEyes = null; 107 private ImageDraw mImageDraw = null; 108 private ImageStraighten mImageStraighten = null; 109 private ImageCrop mImageCrop = null; 110 private ImageRotate mImageRotate = null; 111 private ImageFlip mImageFlip = null; 112 private ImageTinyPlanet mImageTinyPlanet = null; 113 114 private View mListFx = null; 115 private View mListBorders = null; 116 private View mListGeometry = null; 117 private View mListColors = null; 118 private View mListFilterButtons = null; 119 private View mSaveButton = null; 120 121 private ImageButton mFxButton = null; 122 private ImageButton mBorderButton = null; 123 private ImageButton mGeometryButton = null; 124 private ImageButton mColorsButton = null; 125 126 private LinearLayout listColors = null; 127 private LinearLayout listFilters = null; 128 private LinearLayout listBorders = null; 129 130 private EditorPlaceHolder mEditorPlaceHolder = new EditorPlaceHolder(this); 131 132 private static final int SELECT_PICTURE = 1; 133 private static final String LOGTAG = "FilterShowActivity"; 134 protected static final boolean ANIMATE_PANELS = true; 135 private static int mImageBorderSize = 4; // in percent 136 137 private boolean mShowingHistoryPanel = false; 138 private boolean mShowingImageStatePanel = false; 139 140 private final Vector<ImageShow> mImageViews = new Vector<ImageShow>(); 141 private final Vector<View> mListViews = new Vector<View>(); 142 private final Vector<ImageButton> mBottomPanelButtons = new Vector<ImageButton>(); 143 144 private ShareActionProvider mShareActionProvider; 145 private File mSharedOutputFile = null; 146 147 private boolean mSharingImage = false; 148 149 private WeakReference<ProgressDialog> mSavingProgressDialog; 150 private static final int SEEK_BAR_MAX = 600; 151 152 private LoadBitmapTask mLoadBitmapTask; 153 private FilterIconButton mNullFxFilter; 154 private FilterIconButton mNullBorderFilter; 155 private int mIconSeedSize = 140; 156 157 @Override 158 public void onCreate(Bundle savedInstanceState) { 159 super.onCreate(savedInstanceState); 160 setResources(); 161 162 Resources res = getResources(); 163 setupMasterImage(); 164 ImageFilterRS.setRenderScriptContext(this); 165 166 ImageShow.setDefaultBackgroundColor(res.getColor(R.color.background_screen)); 167 // TODO: get those values from XML. 168 ImageZoom.setZoomedSize(getPixelsFromDip(256)); 169 FramedTextButton.setTextSize((int) getPixelsFromDip(14)); 170 FramedTextButton.setTrianglePadding((int) getPixelsFromDip(4)); 171 FramedTextButton.setTriangleSize((int) getPixelsFromDip(10)); 172 ImageShow.setTextSize((int) getPixelsFromDip(12)); 173 ImageShow.setTextPadding((int) getPixelsFromDip(10)); 174 ImageShow.setOriginalTextMargin((int) getPixelsFromDip(4)); 175 ImageShow.setOriginalTextSize((int) getPixelsFromDip(18)); 176 ImageShow.setOriginalText(res.getString(R.string.original_picture_text)); 177 mIconSeedSize = res.getDimensionPixelSize(R.dimen.thumbnail_size); 178 179 Drawable curveHandle = res.getDrawable(R.drawable.camera_crop); 180 int curveHandleSize = (int) res.getDimension(R.dimen.crop_indicator_size); 181 Spline.setCurveHandle(curveHandle, curveHandleSize); 182 Spline.setCurveWidth((int) getPixelsFromDip(3)); 183 184 setContentView(R.layout.filtershow_activity); 185 ActionBar actionBar = getActionBar(); 186 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 187 actionBar.setCustomView(R.layout.filtershow_actionbar); 188 189 mSaveButton = actionBar.getCustomView(); 190 mSaveButton.setOnClickListener(new OnClickListener() { 191 @Override 192 public void onClick(View view) { 193 saveImage(); 194 } 195 }); 196 197 mImageLoader = new ImageLoader(this, getApplicationContext()); 198 199 listFilters = (LinearLayout) findViewById(R.id.listFilters); 200 listBorders = (LinearLayout) findViewById(R.id.listBorders); 201 listColors = (LinearLayout) findViewById(R.id.listColorsFx); 202 203 mImageShow = (ImageShow) findViewById(R.id.imageShow); 204 mImageStraighten = (ImageStraighten) findViewById(R.id.imageStraighten); 205 mImageCrop = (ImageCrop) findViewById(R.id.imageCrop); 206 mImageRotate = (ImageRotate) findViewById(R.id.imageRotate); 207 mImageFlip = (ImageFlip) findViewById(R.id.imageFlip); 208 mImageTinyPlanet = (ImageTinyPlanet) findViewById(R.id.imageTinyPlanet); 209 mImageRedEyes = (ImageRedEyes) findViewById(R.id.imageRedEyes); 210 mImageDraw = (ImageDraw) findViewById(R.id.imageDraw); 211 212 mImageCrop.setAspectTextSize((int) getPixelsFromDip(18)); 213 ImageCrop.setTouchTolerance((int) getPixelsFromDip(25)); 214 ImageCrop.setMinCropSize((int) getPixelsFromDip(55)); 215 mImageViews.add(mImageShow); 216 mImageViews.add(mImageStraighten); 217 mImageViews.add(mImageCrop); 218 mImageViews.add(mImageRotate); 219 mImageViews.add(mImageFlip); 220 mImageViews.add(mImageTinyPlanet); 221 mImageViews.add(mImageRedEyes); 222 223 mEditorPlaceHolder.setContainer((FrameLayout) findViewById(R.id.editorContainer)); 224 mEditorPlaceHolder.addEditor(new EditorDraw()); 225 mEditorPlaceHolder.addEditor(new BasicEditor()); 226 EditorManager.addEditors(mEditorPlaceHolder); 227 mEditorPlaceHolder.setOldViews(mImageViews); 228 mEditorPlaceHolder.setImageLoader(mImageLoader); 229 230 mListFx = findViewById(R.id.fxList); 231 mListBorders = findViewById(R.id.bordersList); 232 mListGeometry = findViewById(R.id.geometryList); 233 mListFilterButtons = findViewById(R.id.filterButtonsList); 234 mListColors = findViewById(R.id.colorsFxList); 235 mListViews.add(mListFx); 236 mListViews.add(mListBorders); 237 mListViews.add(mListGeometry); 238 mListViews.add(mListFilterButtons); 239 mListViews.add(mListColors); 240 241 mFxButton = (ImageButton) findViewById(R.id.fxButton); 242 mBorderButton = (ImageButton) findViewById(R.id.borderButton); 243 mGeometryButton = (ImageButton) findViewById(R.id.geometryButton); 244 mColorsButton = (ImageButton) findViewById(R.id.colorsButton); 245 246 mBottomPanelButtons.add(mFxButton); 247 mBottomPanelButtons.add(mBorderButton); 248 mBottomPanelButtons.add(mGeometryButton); 249 mBottomPanelButtons.add(mColorsButton); 250 251 mImageShow.setImageLoader(mImageLoader); 252 mImageStraighten.setImageLoader(mImageLoader); 253 mImageCrop.setImageLoader(mImageLoader); 254 mImageRotate.setImageLoader(mImageLoader); 255 mImageFlip.setImageLoader(mImageLoader); 256 mImageTinyPlanet.setImageLoader(mImageLoader); 257 mImageRedEyes.setImageLoader(mImageLoader); 258 mImageDraw.setImageLoader(mImageLoader); 259 260 mPanelController.setActivity(this); 261 mPanelController.setEditorPlaceHolder(mEditorPlaceHolder); 262 263 mPanelController.addImageView(findViewById(R.id.imageShow)); 264 mPanelController.addImageView(findViewById(R.id.imageStraighten)); 265 mPanelController.addImageView(findViewById(R.id.imageCrop)); 266 mPanelController.addImageView(findViewById(R.id.imageRotate)); 267 mPanelController.addImageView(findViewById(R.id.imageFlip)); 268 mPanelController.addImageView(findViewById(R.id.imageTinyPlanet)); 269 mPanelController.addImageView(findViewById(R.id.imageRedEyes)); 270 mPanelController.addImageView(findViewById(R.id.imageDraw)); 271 272 mPanelController.addPanel(mFxButton, mListFx, 0); 273 mPanelController.addPanel(mBorderButton, mListBorders, 1); 274 275 mPanelController.addPanel(mGeometryButton, mListGeometry, 2); 276 mPanelController.addComponent(mGeometryButton, findViewById(R.id.straightenButton)); 277 mPanelController.addComponent(mGeometryButton, findViewById(R.id.cropButton)); 278 mPanelController.addComponent(mGeometryButton, findViewById(R.id.rotateButton)); 279 mPanelController.addComponent(mGeometryButton, findViewById(R.id.flipButton)); 280 mPanelController.addComponent(mGeometryButton, findViewById(R.id.redEyeButton)); 281 282 mPanelController.addPanel(mColorsButton, mListColors, 3); 283 284 Vector<ImageFilter> filters = new Vector<ImageFilter>(); 285 FiltersManager.addFilters(filters, mImageLoader); 286 287 for (ImageFilter filter : filters) { 288 filter.setName(getString(filter.getTextId())); 289 setupFilterButton(filter, listColors, mColorsButton); 290 } 291 mPanelController.addFilter(new ImageFilterRedEye()); 292 293 mPanelController.addView(findViewById(R.id.applyEffect)); 294 findViewById(R.id.resetOperationsButton).setOnClickListener( 295 createOnClickResetOperationsButton()); 296 297 ListView operationsList = (ListView) findViewById(R.id.operationsList); 298 operationsList.setAdapter(mMasterImage.getHistory()); 299 operationsList.setOnItemClickListener(this); 300 ListView imageStateList = (ListView) findViewById(R.id.imageStateList); 301 imageStateList.setAdapter(mMasterImage.getState()); 302 mImageLoader.setAdapter(mMasterImage.getHistory()); 303 304 fillListImages(listFilters); 305 LoadBordersTask loadBorders = new LoadBordersTask(listBorders); 306 loadBorders.execute(); 307 308 SeekBar seekBar = (SeekBar) findViewById(R.id.filterSeekBar); 309 seekBar.setMax(SEEK_BAR_MAX); 310 311 mImageShow.setSeekBar(seekBar); 312 mImageTinyPlanet.setSeekBar(seekBar); 313 mPanelController.setRowPanel(findViewById(R.id.secondRowPanel)); 314 mPanelController.setUtilityPanel(this, findViewById(R.id.filterButtonsList), 315 findViewById(R.id.panelAccessoryViewList), 316 findViewById(R.id.applyEffect)); 317 318 mPanelController.setCurrentPanel(mFxButton); 319 Intent intent = getIntent(); 320 if (intent.getBooleanExtra(LAUNCH_FULLSCREEN, false)) { 321 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 322 } 323 324 if (intent.getData() != null) { 325 startLoadBitmap(intent.getData()); 326 } else { 327 pickImage(); 328 } 329 330 // Handle behavior for various actions 331 String action = intent.getAction(); 332 if (action.equalsIgnoreCase(CROP_ACTION)) { 333 Bundle extras = intent.getExtras(); 334 if (extras != null) { 335 mCropExtras = new CropExtras(extras.getInt(CropExtras.KEY_OUTPUT_X, 0), 336 extras.getInt(CropExtras.KEY_OUTPUT_Y, 0), 337 extras.getBoolean(CropExtras.KEY_SCALE, true) && 338 extras.getBoolean(CropExtras.KEY_SCALE_UP_IF_NEEDED, false), 339 extras.getInt(CropExtras.KEY_ASPECT_X, 0), 340 extras.getInt(CropExtras.KEY_ASPECT_Y, 0), 341 extras.getBoolean(CropExtras.KEY_SET_AS_WALLPAPER, false), 342 extras.getBoolean(CropExtras.KEY_RETURN_DATA, false), 343 (Uri) extras.getParcelable(MediaStore.EXTRA_OUTPUT), 344 extras.getString(CropExtras.KEY_OUTPUT_FORMAT), 345 extras.getBoolean(CropExtras.KEY_SHOW_WHEN_LOCKED, false), 346 extras.getFloat(CropExtras.KEY_SPOTLIGHT_X), 347 extras.getFloat(CropExtras.KEY_SPOTLIGHT_Y)); 348 349 if (mCropExtras.getShowWhenLocked()) { 350 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 351 } 352 mImageShow.getImagePreset().mGeoData.setCropExtras(mCropExtras); 353 354 mImageCrop.setExtras(mCropExtras); 355 String s = getString(R.string.Fixed); 356 mImageCrop.setAspectString(s); 357 mImageCrop.setCropActionFlag(true); 358 mPanelController.setFixedAspect(mCropExtras.getAspectX() > 0 359 && mCropExtras.getAspectY() > 0); 360 } 361 mPanelController.showComponent(findViewById(R.id.cropButton)); 362 } else if (action.equalsIgnoreCase(TINY_PLANET_ACTION)) { 363 mPanelController.showComponent(findViewById(R.id.tinyplanetButton)); 364 } 365 } 366 367 private void startLoadBitmap(Uri uri) { 368 final View filters = findViewById(R.id.filtersPanel); 369 final View loading = findViewById(R.id.loading); 370 loading.setVisibility(View.VISIBLE); 371 filters.setVisibility(View.INVISIBLE); 372 View tinyPlanetView = findViewById(R.id.tinyplanetButton); 373 if (tinyPlanetView != null) { 374 tinyPlanetView.setVisibility(View.GONE); 375 } 376 mLoadBitmapTask = new LoadBitmapTask(tinyPlanetView); 377 mLoadBitmapTask.execute(uri); 378 } 379 380 private class LoadBordersTask extends AsyncTask<Void, Boolean, Boolean> { 381 Vector<FilterRepresentation> mBorders; 382 LinearLayout mList; 383 384 public LoadBordersTask(LinearLayout list) { 385 mList = list; 386 mBorders = new Vector<FilterRepresentation>(); 387 } 388 389 @Override 390 protected Boolean doInBackground(Void... params) { 391 mBorders.add(new FilterImageBorderRepresentation(0)); 392 mBorders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_4x5)); 393 mBorders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_brush)); 394 mBorders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_grunge)); 395 mBorders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_sumi_e)); 396 mBorders.add(new FilterImageBorderRepresentation(R.drawable.filtershow_border_tape)); 397 mBorders.add(new FilterColorBorderRepresentation(Color.BLACK, mImageBorderSize, 0)); 398 mBorders.add(new FilterColorBorderRepresentation(Color.BLACK, mImageBorderSize, mImageBorderSize)); 399 mBorders.add(new FilterColorBorderRepresentation(Color.WHITE, mImageBorderSize, 0)); 400 mBorders.add(new FilterColorBorderRepresentation(Color.WHITE, mImageBorderSize, mImageBorderSize)); 401 int creamColor = Color.argb(255, 237, 237, 227); 402 mBorders.add(new FilterColorBorderRepresentation(creamColor, mImageBorderSize, 0)); 403 mBorders.add(new FilterColorBorderRepresentation(creamColor, mImageBorderSize, mImageBorderSize)); 404 return true; 405 } 406 407 @Override 408 protected void onPostExecute(Boolean result) { 409 if (!result) { 410 return; 411 } 412 for (int i = 0; i < mBorders.size(); i++) { 413 FilterRepresentation filter = mBorders.elementAt(i); 414 filter.setName(getString(R.string.borders)); 415 if (i == 0) { 416 filter.setName(getString(R.string.none)); 417 } 418 FilterIconButton b = setupFilterRepresentationButton(filter, mList, mBorderButton); 419 if (i == 0) { 420 mNullBorderFilter = b; 421 mNullBorderFilter.setSelected(true); 422 } 423 } 424 } 425 } 426 427 private class LoadBitmapTask extends AsyncTask<Uri, Boolean, Boolean> { 428 View mTinyPlanetButton; 429 int mBitmapSize; 430 431 public LoadBitmapTask(View button) { 432 mTinyPlanetButton = button; 433 mBitmapSize = getScreenImageSize(); 434 } 435 436 @Override 437 protected Boolean doInBackground(Uri... params) { 438 if (!mImageLoader.loadBitmap(params[0], mBitmapSize)) { 439 return false; 440 } 441 publishProgress(mImageLoader.queryLightCycle360()); 442 return true; 443 } 444 445 @Override 446 protected void onProgressUpdate(Boolean... values) { 447 super.onProgressUpdate(values); 448 if (isCancelled()) { 449 return; 450 } 451 final View filters = findViewById(R.id.filtersPanel); 452 final View loading = findViewById(R.id.loading); 453 loading.setVisibility(View.GONE); 454 filters.setVisibility(View.VISIBLE); 455 if (values[0]) { 456 mTinyPlanetButton.setVisibility(View.VISIBLE); 457 } 458 } 459 460 @Override 461 protected void onPostExecute(Boolean result) { 462 463 if (isCancelled()) { 464 return; 465 } 466 467 if (!result) { 468 cannotLoadImage(); 469 } 470 471 Bitmap largeBitmap = mImageLoader.getOriginalBitmapLarge(); 472 FilteringPipeline pipeline = FilteringPipeline.getPipeline(); 473 pipeline.setOriginal(largeBitmap); 474 float previewScale = (float) largeBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width(); 475 pipeline.setPreviewScaleFactor(previewScale); 476 477 Bitmap bmap = mImageLoader.getOriginalBitmapSmall(); 478 if (bmap != null && bmap.getWidth() > 0 && bmap.getHeight() > 0) { 479 float w = bmap.getWidth(); 480 float h = bmap.getHeight(); 481 float f = mIconSeedSize / Math.min(w, h); 482 w = w * f; 483 h = h * f; 484 bmap = Bitmap.createScaledBitmap(bmap, (int) w, (int) h, true); 485 486 int num_colors_buttons = listColors.getChildCount(); 487 for (int i = 0; i < num_colors_buttons; i++) { 488 FilterIconButton b = (FilterIconButton) listColors.getChildAt(i); 489 490 b.setIcon(bmap); 491 } 492 int num_filters_buttons = listFilters.getChildCount(); 493 for (int i = 0; i < num_filters_buttons; i++) { 494 FilterIconButton b = (FilterIconButton) listFilters.getChildAt(i); 495 496 b.setIcon(bmap); 497 } 498 int num_borders_buttons = listBorders.getChildCount(); 499 for (int i = 0; i < num_borders_buttons; i++) { 500 FilterIconButton b = (FilterIconButton) listBorders.getChildAt(i); 501 502 b.setIcon(bmap); 503 } 504 505 } 506 mLoadBitmapTask = null; 507 super.onPostExecute(result); 508 } 509 510 } 511 512 @Override 513 protected void onDestroy() { 514 if (mLoadBitmapTask != null) { 515 mLoadBitmapTask.cancel(false); 516 } 517 super.onDestroy(); 518 } 519 520 private int translateMainPanel(View viewPanel) { 521 int accessoryPanelWidth = viewPanel.getWidth(); 522 int mainViewWidth = findViewById(R.id.mainView).getWidth(); 523 int mainPanelWidth = mImageShow.getDisplayedImageBounds().width(); 524 if (mainPanelWidth == 0) { 525 mainPanelWidth = mainViewWidth; 526 } 527 int filtersPanelWidth = findViewById(R.id.filtersPanel).getWidth(); 528 if (mainPanelWidth < filtersPanelWidth) { 529 mainPanelWidth = filtersPanelWidth; 530 } 531 int leftOver = mainViewWidth - mainPanelWidth - accessoryPanelWidth; 532 if (leftOver < 0) { 533 return -accessoryPanelWidth; 534 } 535 return 0; 536 } 537 538 private int getScreenImageSize() { 539 DisplayMetrics metrics = new DisplayMetrics(); 540 Display display = getWindowManager().getDefaultDisplay(); 541 Point size = new Point(); 542 display.getSize(size); 543 display.getMetrics(metrics); 544 int msize = Math.min(size.x, size.y); 545 return (133 * msize) / metrics.densityDpi; 546 } 547 548 private void showSavingProgress(String albumName) { 549 ProgressDialog progress; 550 if (mSavingProgressDialog != null) { 551 progress = mSavingProgressDialog.get(); 552 if (progress != null) { 553 progress.show(); 554 return; 555 } 556 } 557 // TODO: Allow cancellation of the saving process 558 String progressText; 559 if (albumName == null) { 560 progressText = getString(R.string.saving_image); 561 } else { 562 progressText = getString(R.string.filtershow_saving_image, albumName); 563 } 564 progress = ProgressDialog.show(this, "", progressText, true, false); 565 mSavingProgressDialog = new WeakReference<ProgressDialog>(progress); 566 } 567 568 private void hideSavingProgress() { 569 if (mSavingProgressDialog != null) { 570 ProgressDialog progress = mSavingProgressDialog.get(); 571 if (progress != null) 572 progress.dismiss(); 573 } 574 } 575 576 public void completeSaveImage(Uri saveUri) { 577 if (mSharingImage && mSharedOutputFile != null) { 578 // Image saved, we unblock the content provider 579 Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI, 580 Uri.encode(mSharedOutputFile.getAbsolutePath())); 581 ContentValues values = new ContentValues(); 582 values.put(SharedImageProvider.PREPARE, false); 583 getContentResolver().insert(uri, values); 584 } 585 setResult(RESULT_OK, new Intent().setData(saveUri)); 586 hideSavingProgress(); 587 finish(); 588 } 589 590 @Override 591 public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) { 592 // First, let's tell the SharedImageProvider that it will need to wait 593 // for the image 594 Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI, 595 Uri.encode(mSharedOutputFile.getAbsolutePath())); 596 ContentValues values = new ContentValues(); 597 values.put(SharedImageProvider.PREPARE, true); 598 getContentResolver().insert(uri, values); 599 mSharingImage = true; 600 601 // Process and save the image in the background. 602 showSavingProgress(null); 603 mImageShow.saveImage(this, mSharedOutputFile); 604 return true; 605 } 606 607 private Intent getDefaultShareIntent() { 608 Intent intent = new Intent(Intent.ACTION_SEND); 609 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 610 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 611 intent.setType(SharedImageProvider.MIME_TYPE); 612 mSharedOutputFile = SaveCopyTask.getNewFile(this, mImageLoader.getUri()); 613 Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI, 614 Uri.encode(mSharedOutputFile.getAbsolutePath())); 615 intent.putExtra(Intent.EXTRA_STREAM, uri); 616 return intent; 617 } 618 619 @Override 620 public boolean onCreateOptionsMenu(Menu menu) { 621 getMenuInflater().inflate(R.menu.filtershow_activity_menu, menu); 622 MenuItem showHistory = menu.findItem(R.id.operationsButton); 623 if (mShowingHistoryPanel) { 624 showHistory.setTitle(R.string.hide_history_panel); 625 } else { 626 showHistory.setTitle(R.string.show_history_panel); 627 } 628 MenuItem showState = menu.findItem(R.id.showImageStateButton); 629 if (mShowingImageStatePanel) { 630 showState.setTitle(R.string.hide_imagestate_panel); 631 } else { 632 showState.setTitle(R.string.show_imagestate_panel); 633 } 634 mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share) 635 .getActionProvider(); 636 mShareActionProvider.setShareIntent(getDefaultShareIntent()); 637 mShareActionProvider.setOnShareTargetSelectedListener(this); 638 639 MenuItem undoItem = menu.findItem(R.id.undoButton); 640 MenuItem redoItem = menu.findItem(R.id.redoButton); 641 MenuItem resetItem = menu.findItem(R.id.resetHistoryButton); 642 mMasterImage.getHistory().setMenuItems(undoItem, redoItem, resetItem); 643 return true; 644 } 645 646 @Override 647 public void onPause() { 648 super.onPause(); 649 if (mShareActionProvider != null) { 650 mShareActionProvider.setOnShareTargetSelectedListener(null); 651 } 652 } 653 654 @Override 655 public void onResume() { 656 super.onResume(); 657 if (mShareActionProvider != null) { 658 mShareActionProvider.setOnShareTargetSelectedListener(this); 659 } 660 } 661 662 @Override 663 public boolean onOptionsItemSelected(MenuItem item) { 664 switch (item.getItemId()) { 665 case R.id.undoButton: { 666 HistoryAdapter adapter = mMasterImage.getHistory(); 667 int position = adapter.undo(); 668 mMasterImage.onHistoryItemClick(position); 669 mImageShow.showToast("Undo"); 670 invalidateViews(); 671 return true; 672 } 673 case R.id.redoButton: { 674 HistoryAdapter adapter = mMasterImage.getHistory(); 675 int position = adapter.redo(); 676 mMasterImage.onHistoryItemClick(position); 677 mImageShow.showToast("Redo"); 678 invalidateViews(); 679 return true; 680 } 681 case R.id.resetHistoryButton: { 682 resetHistory(); 683 return true; 684 } 685 case R.id.showImageStateButton: { 686 toggleImageStatePanel(); 687 return true; 688 } 689 case R.id.operationsButton: { 690 toggleHistoryPanel(); 691 return true; 692 } 693 case android.R.id.home: { 694 saveImage(); 695 return true; 696 } 697 } 698 return false; 699 } 700 701 public void enableSave(boolean enable) { 702 if (mSaveButton != null) 703 mSaveButton.setEnabled(enable); 704 } 705 706 public FilterIconButton setupFilterRepresentationButton(FilterRepresentation representation, LinearLayout panel, View button) { 707 LayoutInflater inflater = 708 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 709 FilterIconButton icon = (FilterIconButton) inflater.inflate(R.layout.filtericonbutton, 710 panel, false); 711 String text = representation.getName(); 712 icon.setup(text, this, panel); 713 icon.setFilterRepresentation(representation); 714 mPanelController.addComponent(button, icon); 715 panel.addView(icon); 716 return icon; 717 } 718 719 public FilterIconButton setupFilterButton(ImageFilter filter, LinearLayout panel, View button) { 720 LayoutInflater inflater = 721 (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 722 FilterIconButton icon = (FilterIconButton) inflater.inflate(R.layout.filtericonbutton, 723 panel, false); 724 String text = filter.getName(); 725 if (filter instanceof ImageFilterBorder || filter instanceof ImageFilterParametricBorder) { 726 text = ""; 727 } 728 icon.setup(text, filter, this, panel); 729 // TODO: get rid of hasDefaultRepresentation() 730 if (filter.hasDefaultRepresentation()) { 731 icon.setFilterRepresentation(filter.getDefaultRepresentation()); 732 } 733 icon.setId(filter.getButtonId()); 734 mPanelController.addComponent(button, icon); 735 mPanelController.addFilter(filter); 736 panel.addView(icon); 737 return icon; 738 } 739 740 private void fillListImages(LinearLayout listFilters) { 741 // TODO: use listview 742 // TODO: load the filters straight from the filesystem 743 744 FilterFxRepresentation[] fxArray = new FilterFxRepresentation[18]; 745 int p = 0; 746 747 int[] drawid = { 748 R.drawable.filtershow_fx_0005_punch, 749 R.drawable.filtershow_fx_0000_vintage, 750 R.drawable.filtershow_fx_0004_bw_contrast, 751 R.drawable.filtershow_fx_0002_bleach, 752 R.drawable.filtershow_fx_0001_instant, 753 R.drawable.filtershow_fx_0007_washout, 754 R.drawable.filtershow_fx_0003_blue_crush, 755 R.drawable.filtershow_fx_0008_washout_color, 756 R.drawable.filtershow_fx_0006_x_process 757 }; 758 759 int[] fxNameid = { 760 R.string.ffx_punch, 761 R.string.ffx_vintage, 762 R.string.ffx_bw_contrast, 763 R.string.ffx_bleach, 764 R.string.ffx_instant, 765 R.string.ffx_washout, 766 R.string.ffx_blue_crush, 767 R.string.ffx_washout_color, 768 R.string.ffx_x_process 769 }; 770 771 ImagePreset preset = new ImagePreset(getString(R.string.history_original)); // empty 772 preset.setImageLoader(mImageLoader); 773 774 BitmapFactory.Options o = new BitmapFactory.Options(); 775 o.inScaled = false; 776 777 for (int i = 0; i < drawid.length; i++) { 778 Bitmap b = BitmapFactory.decodeResource(getResources(), drawid[i], o); 779 FilterFxRepresentation fx = new FilterFxRepresentation(getString(fxNameid[i]), drawid[i], fxNameid[i]); 780 fx.setFxBitmap(b); 781 fxArray[p++] = fx; 782 } 783 784 FilterFxRepresentation nullFx = new FilterFxRepresentation(getString(R.string.none), 0, R.string.none); 785 mNullFxFilter = setupFilterRepresentationButton(nullFx, listFilters, mFxButton); 786 mNullFxFilter.setSelected(true); 787 788 for (int i = 0; i < p; i++) { 789 setupFilterRepresentationButton(fxArray[i], listFilters, mFxButton); 790 } 791 792 // Default preset (original) 793 mMasterImage.setPreset(preset, true); 794 } 795 796 // ////////////////////////////////////////////////////////////////////////////// 797 // Some utility functions 798 // TODO: finish the cleanup. 799 800 public void showOriginalViews(boolean value) { 801 for (ImageShow views : mImageViews) { 802 views.showOriginal(value); 803 } 804 } 805 806 public void invalidateViews() { 807 for (ImageShow views : mImageViews) { 808 views.invalidate(); 809 views.updateImage(); 810 } 811 } 812 813 public void hideListViews() { 814 for (View view : mListViews) { 815 view.setVisibility(View.GONE); 816 } 817 } 818 819 public void hideImageViews() { 820 mImageShow.setShowControls(false); // reset 821 for (View view : mImageViews) { 822 view.setVisibility(View.GONE); 823 } 824 mEditorPlaceHolder.hide(); 825 } 826 827 public void unselectBottomPanelButtons() { 828 for (ImageButton button : mBottomPanelButtons) { 829 button.setSelected(false); 830 } 831 } 832 833 public void unselectPanelButtons(Vector<ImageButton> buttons) { 834 for (ImageButton button : buttons) { 835 button.setSelected(false); 836 } 837 } 838 839 public void disableFilterButtons() { 840 for (ImageButton b : mBottomPanelButtons) { 841 b.setEnabled(false); 842 b.setClickable(false); 843 b.setAlpha(0.4f); 844 } 845 } 846 847 public void enableFilterButtons() { 848 for (ImageButton b : mBottomPanelButtons) { 849 b.setEnabled(true); 850 b.setClickable(true); 851 b.setAlpha(1.0f); 852 } 853 } 854 855 // ////////////////////////////////////////////////////////////////////////////// 856 // imageState panel... 857 858 public boolean isShowingHistoryPanel() { 859 return mShowingHistoryPanel; 860 } 861 862 private void toggleImageStatePanel() { 863 final View view = findViewById(R.id.mainPanel); 864 final View viewList = findViewById(R.id.imageStatePanel); 865 866 if (mShowingHistoryPanel) { 867 findViewById(R.id.historyPanel).setVisibility(View.INVISIBLE); 868 mShowingHistoryPanel = false; 869 } 870 871 int translate = translateMainPanel(viewList); 872 if (!mShowingImageStatePanel) { 873 mShowingImageStatePanel = true; 874 view.animate().setDuration(200).x(translate) 875 .withLayer().withEndAction(new Runnable() { 876 @Override 877 public void run() { 878 viewList.setAlpha(0); 879 viewList.setVisibility(View.VISIBLE); 880 viewList.animate().setDuration(100) 881 .alpha(1.0f).start(); 882 } 883 }).start(); 884 } else { 885 mShowingImageStatePanel = false; 886 viewList.setVisibility(View.INVISIBLE); 887 view.animate().setDuration(200).x(0).withLayer() 888 .start(); 889 } 890 invalidateOptionsMenu(); 891 } 892 893 @Override 894 public void onConfigurationChanged(Configuration newConfig) 895 { 896 super.onConfigurationChanged(newConfig); 897 setResources(); 898 if (mShowingHistoryPanel) { 899 toggleHistoryPanel(); 900 } 901 } 902 903 public void setupMasterImage() { 904 HistoryAdapter mHistoryAdapter = new HistoryAdapter( 905 this, R.layout.filtershow_history_operation_row, 906 R.id.rowTextView); 907 ImageStateAdapter mImageStateAdapter = new ImageStateAdapter(this, 908 R.layout.filtershow_imagestate_row); 909 910 MasterImage.reset(); 911 mMasterImage = MasterImage.getImage(); 912 mMasterImage.setHistoryAdapter(mHistoryAdapter); 913 mMasterImage.setStateAdapter(mImageStateAdapter); 914 mMasterImage.setActivity(this); 915 } 916 917 // ////////////////////////////////////////////////////////////////////////////// 918 // history panel... 919 920 public void toggleHistoryPanel() { 921 final View view = findViewById(R.id.mainPanel); 922 final View viewList = findViewById(R.id.historyPanel); 923 924 if (mShowingImageStatePanel) { 925 findViewById(R.id.imageStatePanel).setVisibility(View.INVISIBLE); 926 mShowingImageStatePanel = false; 927 } 928 929 int translate = translateMainPanel(viewList); 930 if (!mShowingHistoryPanel) { 931 mShowingHistoryPanel = true; 932 view.animate().setDuration(200).x(translate) 933 .withLayer().withEndAction(new Runnable() { 934 @Override 935 public void run() { 936 viewList.setAlpha(0); 937 viewList.setVisibility(View.VISIBLE); 938 viewList.animate().setDuration(100) 939 .alpha(1.0f).start(); 940 } 941 }).start(); 942 } else { 943 mShowingHistoryPanel = false; 944 viewList.setVisibility(View.INVISIBLE); 945 view.animate().setDuration(200).x(0).withLayer() 946 .start(); 947 } 948 invalidateOptionsMenu(); 949 } 950 951 public void dispatchNullFilterClick() { 952 mNullFxFilter.onClick(mNullFxFilter); 953 mNullBorderFilter.onClick(mNullBorderFilter); 954 } 955 956 void resetHistory() { 957 dispatchNullFilterClick(); 958 HistoryAdapter adapter = mMasterImage.getHistory(); 959 adapter.reset(); 960 ImagePreset original = new ImagePreset(adapter.getItem(0)); 961 mMasterImage.setPreset(original, true); 962 mPanelController.resetParameters(); 963 invalidateViews(); 964 } 965 966 // reset button in the history panel. 967 private OnClickListener createOnClickResetOperationsButton() { 968 return new View.OnClickListener() { 969 @Override 970 public void onClick(View v) { 971 resetHistory(); 972 } 973 }; 974 } 975 976 @Override 977 public void onBackPressed() { 978 if (mPanelController.onBackPressed()) { 979 saveImage(); 980 } 981 } 982 983 public void cannotLoadImage() { 984 CharSequence text = getString(R.string.cannot_load_image); 985 Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT); 986 toast.show(); 987 finish(); 988 } 989 990 // ////////////////////////////////////////////////////////////////////////////// 991 992 public float getPixelsFromDip(float value) { 993 Resources r = getResources(); 994 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, 995 r.getDisplayMetrics()); 996 } 997 998 public void useFilterRepresentation(FilterRepresentation filterRepresentation) { 999 if (filterRepresentation == null) { 1000 return; 1001 } 1002 if (mMasterImage.getCurrentFilterRepresentation() == filterRepresentation) { 1003 return; 1004 } 1005 ImagePreset oldPreset = mMasterImage.getPreset(); 1006 ImagePreset copy = new ImagePreset(oldPreset); 1007 FilterRepresentation representation = copy.getRepresentation(filterRepresentation); 1008 if (representation == null) { 1009 copy.addFilter(filterRepresentation); 1010 } else { 1011 if (filterRepresentation.allowsMultipleInstances()) { 1012 representation.useParametersFrom(filterRepresentation); 1013 copy.setHistoryName(filterRepresentation.getName()); 1014 } 1015 filterRepresentation = representation; 1016 } 1017 mMasterImage.setPreset(copy, true); 1018 mMasterImage.setCurrentFilterRepresentation(filterRepresentation); 1019 invalidateViews(); 1020 } 1021 1022 @Override 1023 public void onItemClick(AdapterView<?> parent, View view, int position, 1024 long id) { 1025 mMasterImage.onHistoryItemClick(position); 1026 invalidateViews(); 1027 } 1028 1029 public void pickImage() { 1030 Intent intent = new Intent(); 1031 intent.setType("image/*"); 1032 intent.setAction(Intent.ACTION_GET_CONTENT); 1033 startActivityForResult(Intent.createChooser(intent, getString(R.string.select_image)), 1034 SELECT_PICTURE); 1035 } 1036 1037 @Override 1038 public void onActivityResult(int requestCode, int resultCode, Intent data) { 1039 if (resultCode == RESULT_OK) { 1040 if (requestCode == SELECT_PICTURE) { 1041 Uri selectedImageUri = data.getData(); 1042 startLoadBitmap(selectedImageUri); 1043 } 1044 } 1045 } 1046 1047 private boolean mSaveToExtraUri = false; 1048 private boolean mSaveAsWallpaper = false; 1049 private boolean mReturnAsExtra = false; 1050 private boolean outputted = false; 1051 1052 public void saveImage() { 1053 if (mCropExtras != null) { 1054 if (mCropExtras.getExtraOutput() != null) { 1055 mSaveToExtraUri = true; 1056 outputted = true; 1057 } 1058 if (mCropExtras.getSetAsWallpaper()) { 1059 mSaveAsWallpaper = true; 1060 outputted = true; 1061 } 1062 if (mCropExtras.getReturnData()) { 1063 1064 mReturnAsExtra = true; 1065 outputted = true; 1066 } 1067 1068 if (outputted) { 1069 mImageShow.getImagePreset().mGeoData.setUseCropExtrasFlag(true); 1070 showSavingProgress(null); 1071 mImageShow.returnFilteredResult(this); 1072 } 1073 } 1074 if (!outputted) { 1075 if (mImageShow.hasModifications()) { 1076 // Get the name of the album, to which the image will be saved 1077 File saveDir = SaveCopyTask.getFinalSaveDirectory(this, mImageLoader.getUri()); 1078 int bucketId = GalleryUtils.getBucketId(saveDir.getPath()); 1079 String albumName = LocalAlbum.getLocalizedName(getResources(), bucketId, null); 1080 showSavingProgress(albumName); 1081 mImageShow.saveImage(this, null); 1082 } else { 1083 done(); 1084 } 1085 } 1086 } 1087 1088 public void onFilteredResult(Bitmap filtered) { 1089 Intent intent = new Intent(); 1090 intent.putExtra(CropExtras.KEY_CROPPED_RECT, mImageShow.getImageCropBounds()); 1091 if (mSaveToExtraUri) { 1092 mImageShow.saveToUri(filtered, mCropExtras.getExtraOutput(), 1093 mCropExtras.getOutputFormat(), this); 1094 } 1095 if (mSaveAsWallpaper) { 1096 try { 1097 WallpaperManager.getInstance(this).setBitmap(filtered); 1098 } catch (IOException e) { 1099 Log.w(LOGTAG, "fail to set wall paper", e); 1100 } 1101 } 1102 if (mReturnAsExtra) { 1103 if (filtered != null) { 1104 int bmapSize = filtered.getRowBytes() * filtered.getHeight(); 1105 /* 1106 * Max size of Binder transaction buffer is 1Mb, so constrain 1107 * Bitmap to be somewhat less than this, otherwise we get 1108 * TransactionTooLargeExceptions. 1109 */ 1110 if (bmapSize > MAX_BMAP_IN_INTENT) { 1111 Log.w(LOGTAG, "Bitmap too large to be returned via intent"); 1112 } else { 1113 intent.putExtra(CropExtras.KEY_DATA, filtered); 1114 } 1115 } 1116 } 1117 setResult(RESULT_OK, intent); 1118 if (!mSaveToExtraUri) { 1119 done(); 1120 } 1121 } 1122 1123 public void done() { 1124 if (outputted) { 1125 hideSavingProgress(); 1126 } 1127 finish(); 1128 } 1129 1130 private void setResources() { 1131 ImageFilterBorder filterBorder = (ImageFilterBorder) FiltersManager.getManager().getFilter(ImageFilterBorder.class); 1132 filterBorder.setResources(getResources()); 1133 } 1134 1135 static { 1136 System.loadLibrary("jni_filtershow_filters"); 1137 } 1138 1139} 1140