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