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