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