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 17 package com.android.gallery3d.filtershow; 18 19 import android.app.ActionBar; 20 import android.app.AlertDialog; 21 import android.app.ProgressDialog; 22 import android.app.WallpaperManager; 23 import android.content.ContentValues; 24 import android.content.DialogInterface; 25 import android.content.Intent; 26 import android.content.pm.ActivityInfo; 27 import android.content.res.Configuration; 28 import android.content.res.Resources; 29 import android.graphics.Bitmap; 30 import android.graphics.Point; 31 import android.graphics.drawable.Drawable; 32 import android.net.Uri; 33 import android.os.AsyncTask; 34 import android.os.Bundle; 35 import android.support.v4.app.Fragment; 36 import android.support.v4.app.FragmentActivity; 37 import android.support.v4.app.FragmentTransaction; 38 import android.util.DisplayMetrics; 39 import android.util.Log; 40 import android.util.TypedValue; 41 import android.view.Display; 42 import android.view.Menu; 43 import android.view.MenuItem; 44 import android.view.View; 45 import android.view.View.OnClickListener; 46 import android.view.WindowManager; 47 import android.widget.AdapterView; 48 import android.widget.AdapterView.OnItemClickListener; 49 import android.widget.FrameLayout; 50 import android.widget.ShareActionProvider; 51 import android.widget.ShareActionProvider.OnShareTargetSelectedListener; 52 53 import android.widget.Toast; 54 import com.android.gallery3d.R; 55 import com.android.gallery3d.data.LocalAlbum; 56 import com.android.gallery3d.filtershow.cache.CachingPipeline; 57 import com.android.gallery3d.filtershow.cache.FilteringPipeline; 58 import com.android.gallery3d.filtershow.cache.ImageLoader; 59 import com.android.gallery3d.filtershow.category.*; 60 import com.android.gallery3d.filtershow.crop.CropExtras; 61 import com.android.gallery3d.filtershow.editors.*; 62 import com.android.gallery3d.filtershow.filters.*; 63 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata; 64 import com.android.gallery3d.filtershow.imageshow.ImageCrop; 65 import com.android.gallery3d.filtershow.imageshow.ImageShow; 66 import com.android.gallery3d.filtershow.imageshow.MasterImage; 67 import com.android.gallery3d.filtershow.presets.ImagePreset; 68 import com.android.gallery3d.filtershow.provider.SharedImageProvider; 69 import com.android.gallery3d.filtershow.state.StateAdapter; 70 import com.android.gallery3d.filtershow.tools.BitmapTask; 71 import com.android.gallery3d.filtershow.tools.SaveCopyTask; 72 import com.android.gallery3d.filtershow.ui.FramedTextButton; 73 import com.android.gallery3d.filtershow.ui.Spline; 74 import com.android.gallery3d.util.GalleryUtils; 75 import com.android.photos.data.GalleryBitmapPool; 76 77 import java.io.File; 78 import java.io.IOException; 79 import java.lang.ref.WeakReference; 80 import java.util.Vector; 81 82 public class FilterShowActivity extends FragmentActivity implements OnItemClickListener, 83 OnShareTargetSelectedListener { 84 85 // fields for supporting crop action 86 public static final String CROP_ACTION = "com.android.camera.action.CROP"; 87 private CropExtras mCropExtras = null; 88 private String mAction = ""; 89 MasterImage mMasterImage = null; 90 91 private static final long LIMIT_SUPPORTS_HIGHRES = 134217728; // 128Mb 92 93 public static final String TINY_PLANET_ACTION = "com.android.camera.action.TINY_PLANET"; 94 public static final String LAUNCH_FULLSCREEN = "launch-fullscreen"; 95 public static final int MAX_BMAP_IN_INTENT = 990000; 96 private ImageLoader mImageLoader = null; 97 private ImageShow mImageShow = null; 98 99 private View mSaveButton = null; 100 101 private EditorPlaceHolder mEditorPlaceHolder = new EditorPlaceHolder(this); 102 103 private static final int SELECT_PICTURE = 1; 104 private static final String LOGTAG = "FilterShowActivity"; 105 protected static final boolean ANIMATE_PANELS = true; 106 107 private boolean mShowingTinyPlanet = false; 108 private boolean mShowingImageStatePanel = false; 109 110 private final Vector<ImageShow> mImageViews = new Vector<ImageShow>(); 111 112 private ShareActionProvider mShareActionProvider; 113 private File mSharedOutputFile = null; 114 115 private boolean mSharingImage = false; 116 117 private WeakReference<ProgressDialog> mSavingProgressDialog; 118 119 private LoadBitmapTask mLoadBitmapTask; 120 private boolean mLoading = true; 121 122 private CategoryAdapter mCategoryLooksAdapter = null; 123 private CategoryAdapter mCategoryBordersAdapter = null; 124 private CategoryAdapter mCategoryGeometryAdapter = null; 125 private CategoryAdapter mCategoryFiltersAdapter = null; 126 private int mCurrentPanel = MainPanel.LOOKS; 127 128 @Override 129 public void onCreate(Bundle savedInstanceState) { 130 super.onCreate(savedInstanceState); 131 132 boolean onlyUsePortrait = getResources().getBoolean(R.bool.only_use_portrait); 133 if (onlyUsePortrait) { 134 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 135 } 136 MasterImage.setMaster(mMasterImage); 137 138 clearGalleryBitmapPool(); 139 140 CachingPipeline.createRenderscriptContext(this); 141 setupMasterImage(); 142 setDefaultValues(); 143 fillEditors(); 144 145 loadXML(); 146 loadMainPanel(); 147 148 setDefaultPreset(); 149 150 processIntent(); 151 } 152 153 public boolean isShowingImageStatePanel() { 154 return mShowingImageStatePanel; 155 } 156 157 public void loadMainPanel() { 158 if (findViewById(R.id.main_panel_container) == null) { 159 return; 160 } 161 MainPanel panel = new MainPanel(); 162 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 163 transaction.replace(R.id.main_panel_container, panel, MainPanel.FRAGMENT_TAG); 164 transaction.commit(); 165 } 166 167 public void loadEditorPanel(FilterRepresentation representation, 168 final Editor currentEditor) { 169 if (representation.getEditorId() == ImageOnlyEditor.ID) { 170 currentEditor.getImageShow().select(); 171 currentEditor.reflectCurrentFilter(); 172 return; 173 } 174 final int currentId = currentEditor.getID(); 175 Runnable showEditor = new Runnable() { 176 @Override 177 public void run() { 178 EditorPanel panel = new EditorPanel(); 179 panel.setEditor(currentId); 180 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 181 transaction.remove(getSupportFragmentManager().findFragmentByTag(MainPanel.FRAGMENT_TAG)); 182 transaction.replace(R.id.main_panel_container, panel, MainPanel.FRAGMENT_TAG); 183 transaction.commit(); 184 } 185 }; 186 Fragment main = getSupportFragmentManager().findFragmentByTag(MainPanel.FRAGMENT_TAG); 187 boolean doAnimation = false; 188 if (mShowingImageStatePanel 189 && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 190 doAnimation = true; 191 } 192 if (doAnimation && main != null && main instanceof MainPanel) { 193 MainPanel mainPanel = (MainPanel) main; 194 View container = mainPanel.getView().findViewById(R.id.category_panel_container); 195 View bottom = mainPanel.getView().findViewById(R.id.bottom_panel); 196 int panelHeight = container.getHeight() + bottom.getHeight(); 197 mainPanel.getView().animate().translationY(panelHeight).withEndAction(showEditor).start(); 198 } else { 199 showEditor.run(); 200 } 201 } 202 203 private void loadXML() { 204 setContentView(R.layout.filtershow_activity); 205 206 ActionBar actionBar = getActionBar(); 207 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 208 actionBar.setCustomView(R.layout.filtershow_actionbar); 209 210 mSaveButton = actionBar.getCustomView(); 211 mSaveButton.setOnClickListener(new OnClickListener() { 212 @Override 213 public void onClick(View view) { 214 saveImage(); 215 } 216 }); 217 218 mImageShow = (ImageShow) findViewById(R.id.imageShow); 219 mImageViews.add(mImageShow); 220 221 setupEditors(); 222 223 mEditorPlaceHolder.hide(); 224 225 mImageShow.setImageLoader(mImageLoader); 226 227 fillFx(); 228 fillBorders(); 229 fillGeometry(); 230 fillFilters(); 231 232 setupStatePanel(); 233 } 234 235 public void setupStatePanel() { 236 mImageLoader.setAdapter(mMasterImage.getHistory()); 237 } 238 239 private void fillFilters() { 240 Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>(); 241 FiltersManager filtersManager = FiltersManager.getManager(); 242 filtersManager.addEffects(filtersRepresentations); 243 244 mCategoryFiltersAdapter = new CategoryAdapter(this); 245 for (FilterRepresentation representation : filtersRepresentations) { 246 if (representation.getTextId() != 0) { 247 representation.setName(getString(representation.getTextId())); 248 } 249 mCategoryFiltersAdapter.add(new Action(this, representation)); 250 } 251 } 252 253 private void fillGeometry() { 254 Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>(); 255 FiltersManager filtersManager = FiltersManager.getManager(); 256 257 GeometryMetadata geo = new GeometryMetadata(); 258 int[] editorsId = geo.getEditorIds(); 259 for (int i = 0; i < editorsId.length; i++) { 260 int editorId = editorsId[i]; 261 GeometryMetadata geometry = new GeometryMetadata(geo); 262 geometry.setEditorId(editorId); 263 EditorInfo editorInfo = (EditorInfo) mEditorPlaceHolder.getEditor(editorId); 264 geometry.setTextId(editorInfo.getTextId()); 265 geometry.setOverlayId(editorInfo.getOverlayId()); 266 geometry.setOverlayOnly(editorInfo.getOverlayOnly()); 267 if (geometry.getTextId() != 0) { 268 geometry.setName(getString(geometry.getTextId())); 269 } 270 filtersRepresentations.add(geometry); 271 } 272 273 filtersManager.addTools(filtersRepresentations); 274 275 mCategoryGeometryAdapter = new CategoryAdapter(this); 276 for (FilterRepresentation representation : filtersRepresentations) { 277 mCategoryGeometryAdapter.add(new Action(this, representation)); 278 } 279 } 280 281 private void processIntent() { 282 Intent intent = getIntent(); 283 if (intent.getBooleanExtra(LAUNCH_FULLSCREEN, false)) { 284 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 285 } 286 287 mAction = intent.getAction(); 288 289 if (intent.getData() != null) { 290 startLoadBitmap(intent.getData()); 291 } else { 292 pickImage(); 293 } 294 } 295 296 private void setupEditors() { 297 mEditorPlaceHolder.setContainer((FrameLayout) findViewById(R.id.editorContainer)); 298 EditorManager.addEditors(mEditorPlaceHolder); 299 mEditorPlaceHolder.setOldViews(mImageViews); 300 mEditorPlaceHolder.setImageLoader(mImageLoader); 301 } 302 303 private void fillEditors() { 304 mEditorPlaceHolder.addEditor(new EditorDraw()); 305 mEditorPlaceHolder.addEditor(new BasicEditor()); 306 mEditorPlaceHolder.addEditor(new ImageOnlyEditor()); 307 mEditorPlaceHolder.addEditor(new EditorTinyPlanet()); 308 mEditorPlaceHolder.addEditor(new EditorRedEye()); 309 mEditorPlaceHolder.addEditor(new EditorCrop()); 310 mEditorPlaceHolder.addEditor(new EditorFlip()); 311 mEditorPlaceHolder.addEditor(new EditorRotate()); 312 mEditorPlaceHolder.addEditor(new EditorStraighten()); 313 } 314 315 private void setDefaultValues() { 316 ImageFilter.setActivityForMemoryToasts(this); 317 318 Resources res = getResources(); 319 FiltersManager.setResources(res); 320 321 CategoryView.setMargin((int) getPixelsFromDip(8)); 322 CategoryView.setTextSize((int) getPixelsFromDip(16)); 323 324 ImageShow.setDefaultBackgroundColor(res.getColor(R.color.background_screen)); 325 // TODO: get those values from XML. 326 FramedTextButton.setTextSize((int) getPixelsFromDip(14)); 327 FramedTextButton.setTrianglePadding((int) getPixelsFromDip(4)); 328 FramedTextButton.setTriangleSize((int) getPixelsFromDip(10)); 329 ImageShow.setTextSize((int) getPixelsFromDip(12)); 330 ImageShow.setTextPadding((int) getPixelsFromDip(10)); 331 ImageShow.setOriginalTextMargin((int) getPixelsFromDip(4)); 332 ImageShow.setOriginalTextSize((int) getPixelsFromDip(18)); 333 ImageShow.setOriginalText(res.getString(R.string.original_picture_text)); 334 335 Drawable curveHandle = res.getDrawable(R.drawable.camera_crop); 336 int curveHandleSize = (int) res.getDimension(R.dimen.crop_indicator_size); 337 Spline.setCurveHandle(curveHandle, curveHandleSize); 338 Spline.setCurveWidth((int) getPixelsFromDip(3)); 339 340 ImageCrop.setAspectTextSize((int) getPixelsFromDip(18)); 341 ImageCrop.setTouchTolerance((int) getPixelsFromDip(25)); 342 ImageCrop.setMinCropSize((int) getPixelsFromDip(55)); 343 } 344 345 private void startLoadBitmap(Uri uri) { 346 mLoading = true; 347 final View loading = findViewById(R.id.loading); 348 final View imageShow = findViewById(R.id.imageShow); 349 imageShow.setVisibility(View.INVISIBLE); 350 loading.setVisibility(View.VISIBLE); 351 mShowingTinyPlanet = false; 352 mLoadBitmapTask = new LoadBitmapTask(); 353 mLoadBitmapTask.execute(uri); 354 } 355 356 private void fillBorders() { 357 Vector<FilterRepresentation> borders = new Vector<FilterRepresentation>(); 358 359 // The "no border" implementation 360 borders.add(new FilterImageBorderRepresentation(0)); 361 362 // Google-build borders 363 FiltersManager.getManager().addBorders(this, borders); 364 365 for (int i = 0; i < borders.size(); i++) { 366 FilterRepresentation filter = borders.elementAt(i); 367 if (i == 0) { 368 filter.setName(getString(R.string.none)); 369 } 370 } 371 372 mCategoryBordersAdapter = new CategoryAdapter(this); 373 for (FilterRepresentation representation : borders) { 374 if (representation.getTextId() != 0) { 375 representation.setName(getString(representation.getTextId())); 376 } 377 mCategoryBordersAdapter.add(new Action(this, representation, Action.FULL_VIEW)); 378 } 379 } 380 381 public CategoryAdapter getCategoryLooksAdapter() { 382 return mCategoryLooksAdapter; 383 } 384 385 public CategoryAdapter getCategoryBordersAdapter() { 386 return mCategoryBordersAdapter; 387 } 388 389 public CategoryAdapter getCategoryGeometryAdapter() { 390 return mCategoryGeometryAdapter; 391 } 392 393 public CategoryAdapter getCategoryFiltersAdapter() { 394 return mCategoryFiltersAdapter; 395 } 396 397 public void removeFilterRepresentation(FilterRepresentation filterRepresentation) { 398 if (filterRepresentation == null) { 399 return; 400 } 401 ImagePreset oldPreset = MasterImage.getImage().getPreset(); 402 ImagePreset copy = new ImagePreset(oldPreset); 403 copy.removeFilter(filterRepresentation); 404 MasterImage.getImage().setPreset(copy, true); 405 if (MasterImage.getImage().getCurrentFilterRepresentation() == filterRepresentation) { 406 FilterRepresentation lastRepresentation = copy.getLastRepresentation(); 407 MasterImage.getImage().setCurrentFilterRepresentation(lastRepresentation); 408 } 409 } 410 411 public void useFilterRepresentation(FilterRepresentation filterRepresentation) { 412 if (filterRepresentation == null) { 413 return; 414 } 415 if (MasterImage.getImage().getCurrentFilterRepresentation() == filterRepresentation) { 416 return; 417 } 418 ImagePreset oldPreset = MasterImage.getImage().getPreset(); 419 ImagePreset copy = new ImagePreset(oldPreset); 420 FilterRepresentation representation = copy.getRepresentation(filterRepresentation); 421 if (representation == null) { 422 copy.addFilter(filterRepresentation); 423 } else { 424 if (filterRepresentation.allowsMultipleInstances()) { 425 representation.updateTempParametersFrom(filterRepresentation); 426 copy.setHistoryName(filterRepresentation.getName()); 427 representation.synchronizeRepresentation(); 428 } 429 filterRepresentation = representation; 430 } 431 MasterImage.getImage().setPreset(copy, true); 432 MasterImage.getImage().setCurrentFilterRepresentation(filterRepresentation); 433 } 434 435 public void showRepresentation(FilterRepresentation representation) { 436 if (representation == null) { 437 return; 438 } 439 useFilterRepresentation(representation); 440 441 // show representation 442 Editor mCurrentEditor = mEditorPlaceHolder.showEditor(representation.getEditorId()); 443 loadEditorPanel(representation, mCurrentEditor); 444 } 445 446 public Editor getEditor(int editorID) { 447 return mEditorPlaceHolder.getEditor(editorID); 448 } 449 450 public void setCurrentPanel(int currentPanel) { 451 mCurrentPanel = currentPanel; 452 } 453 454 public int getCurrentPanel() { 455 return mCurrentPanel; 456 } 457 458 private class LoadBitmapTask extends AsyncTask<Uri, Boolean, Boolean> { 459 int mBitmapSize; 460 461 public LoadBitmapTask() { 462 mBitmapSize = getScreenImageSize(); 463 } 464 465 @Override 466 protected Boolean doInBackground(Uri... params) { 467 if (!mImageLoader.loadBitmap(params[0], mBitmapSize)) { 468 return false; 469 } 470 publishProgress(mImageLoader.queryLightCycle360()); 471 return true; 472 } 473 474 @Override 475 protected void onProgressUpdate(Boolean... values) { 476 super.onProgressUpdate(values); 477 if (isCancelled()) { 478 return; 479 } 480 if (values[0]) { 481 mShowingTinyPlanet = true; 482 } 483 } 484 485 @Override 486 protected void onPostExecute(Boolean result) { 487 MasterImage.setMaster(mMasterImage); 488 if (isCancelled()) { 489 return; 490 } 491 492 if (!result) { 493 cannotLoadImage(); 494 } 495 496 final View loading = findViewById(R.id.loading); 497 loading.setVisibility(View.GONE); 498 final View imageShow = findViewById(R.id.imageShow); 499 imageShow.setVisibility(View.VISIBLE); 500 501 Bitmap largeBitmap = mImageLoader.getOriginalBitmapLarge(); 502 FilteringPipeline pipeline = FilteringPipeline.getPipeline(); 503 pipeline.setOriginal(largeBitmap); 504 float previewScale = (float) largeBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width(); 505 pipeline.setPreviewScaleFactor(previewScale); 506 Bitmap highresBitmap = mImageLoader.getOriginalBitmapHighres(); 507 if (highresBitmap != null) { 508 float highResPreviewScale = (float) highresBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width(); 509 pipeline.setHighResPreviewScaleFactor(highResPreviewScale); 510 } 511 if (!mShowingTinyPlanet) { 512 mCategoryFiltersAdapter.removeTinyPlanet(); 513 } 514 pipeline.turnOnPipeline(true); 515 MasterImage.getImage().setOriginalGeometry(largeBitmap); 516 mCategoryLooksAdapter.imageLoaded(); 517 mCategoryBordersAdapter.imageLoaded(); 518 mCategoryGeometryAdapter.imageLoaded(); 519 mCategoryFiltersAdapter.imageLoaded(); 520 mLoadBitmapTask = null; 521 522 if (mAction == TINY_PLANET_ACTION) { 523 showRepresentation(mCategoryFiltersAdapter.getTinyPlanet()); 524 } 525 mLoading = false; 526 super.onPostExecute(result); 527 } 528 529 } 530 531 private void clearGalleryBitmapPool() { 532 (new AsyncTask<Void, Void, Void>() { 533 @Override 534 protected Void doInBackground(Void... params) { 535 // Free memory held in Gallery's Bitmap pool. May be O(n) for n bitmaps. 536 GalleryBitmapPool.getInstance().clear(); 537 return null; 538 } 539 }).execute(); 540 } 541 542 @Override 543 protected void onDestroy() { 544 if (mLoadBitmapTask != null) { 545 mLoadBitmapTask.cancel(false); 546 } 547 // TODO: refactor, don't use so many singletons. 548 FilteringPipeline.getPipeline().turnOnPipeline(false); 549 MasterImage.reset(); 550 FilteringPipeline.reset(); 551 ImageFilter.resetStatics(); 552 FiltersManager.getPreviewManager().freeRSFilterScripts(); 553 FiltersManager.getManager().freeRSFilterScripts(); 554 FiltersManager.getHighresManager().freeRSFilterScripts(); 555 FiltersManager.reset(); 556 CachingPipeline.destroyRenderScriptContext(); 557 super.onDestroy(); 558 } 559 560 private int getScreenImageSize() { 561 DisplayMetrics metrics = new DisplayMetrics(); 562 Display display = getWindowManager().getDefaultDisplay(); 563 Point size = new Point(); 564 display.getSize(size); 565 display.getMetrics(metrics); 566 int msize = Math.min(size.x, size.y); 567 return (133 * msize) / metrics.densityDpi; 568 } 569 570 private void showSavingProgress(String albumName) { 571 ProgressDialog progress; 572 if (mSavingProgressDialog != null) { 573 progress = mSavingProgressDialog.get(); 574 if (progress != null) { 575 progress.show(); 576 return; 577 } 578 } 579 // TODO: Allow cancellation of the saving process 580 String progressText; 581 if (albumName == null) { 582 progressText = getString(R.string.saving_image); 583 } else { 584 progressText = getString(R.string.filtershow_saving_image, albumName); 585 } 586 progress = ProgressDialog.show(this, "", progressText, true, false); 587 mSavingProgressDialog = new WeakReference<ProgressDialog>(progress); 588 } 589 590 private void hideSavingProgress() { 591 if (mSavingProgressDialog != null) { 592 ProgressDialog progress = mSavingProgressDialog.get(); 593 if (progress != null) 594 progress.dismiss(); 595 } 596 } 597 598 public void completeSaveImage(Uri saveUri) { 599 if (mSharingImage && mSharedOutputFile != null) { 600 // Image saved, we unblock the content provider 601 Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI, 602 Uri.encode(mSharedOutputFile.getAbsolutePath())); 603 ContentValues values = new ContentValues(); 604 values.put(SharedImageProvider.PREPARE, false); 605 getContentResolver().insert(uri, values); 606 } 607 setResult(RESULT_OK, new Intent().setData(saveUri)); 608 hideSavingProgress(); 609 finish(); 610 } 611 612 @Override 613 public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) { 614 // First, let's tell the SharedImageProvider that it will need to wait 615 // for the image 616 Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI, 617 Uri.encode(mSharedOutputFile.getAbsolutePath())); 618 ContentValues values = new ContentValues(); 619 values.put(SharedImageProvider.PREPARE, true); 620 getContentResolver().insert(uri, values); 621 mSharingImage = true; 622 623 // Process and save the image in the background. 624 showSavingProgress(null); 625 mImageShow.saveImage(this, mSharedOutputFile); 626 return true; 627 } 628 629 private Intent getDefaultShareIntent() { 630 Intent intent = new Intent(Intent.ACTION_SEND); 631 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 632 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 633 intent.setType(SharedImageProvider.MIME_TYPE); 634 mSharedOutputFile = SaveCopyTask.getNewFile(this, mImageLoader.getUri()); 635 Uri uri = Uri.withAppendedPath(SharedImageProvider.CONTENT_URI, 636 Uri.encode(mSharedOutputFile.getAbsolutePath())); 637 intent.putExtra(Intent.EXTRA_STREAM, uri); 638 return intent; 639 } 640 641 @Override 642 public boolean onCreateOptionsMenu(Menu menu) { 643 getMenuInflater().inflate(R.menu.filtershow_activity_menu, menu); 644 MenuItem showState = menu.findItem(R.id.showImageStateButton); 645 if (mShowingImageStatePanel) { 646 showState.setTitle(R.string.hide_imagestate_panel); 647 } else { 648 showState.setTitle(R.string.show_imagestate_panel); 649 } 650 mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share) 651 .getActionProvider(); 652 mShareActionProvider.setShareIntent(getDefaultShareIntent()); 653 mShareActionProvider.setOnShareTargetSelectedListener(this); 654 655 MenuItem undoItem = menu.findItem(R.id.undoButton); 656 MenuItem redoItem = menu.findItem(R.id.redoButton); 657 MenuItem resetItem = menu.findItem(R.id.resetHistoryButton); 658 mMasterImage.getHistory().setMenuItems(undoItem, redoItem, resetItem); 659 return true; 660 } 661 662 @Override 663 public void onPause() { 664 super.onPause(); 665 rsPause(); 666 if (mShareActionProvider != null) { 667 mShareActionProvider.setOnShareTargetSelectedListener(null); 668 } 669 } 670 671 @Override 672 public void onResume() { 673 super.onResume(); 674 rsResume(); 675 if (mShareActionProvider != null) { 676 mShareActionProvider.setOnShareTargetSelectedListener(this); 677 } 678 } 679 680 private void rsResume() { 681 ImageFilter.setActivityForMemoryToasts(this); 682 MasterImage.setMaster(mMasterImage); 683 if (CachingPipeline.getRenderScriptContext() == null) { 684 CachingPipeline.createRenderscriptContext(this); 685 } 686 FiltersManager.setResources(getResources()); 687 if (!mLoading) { 688 Bitmap largeBitmap = mImageLoader.getOriginalBitmapLarge(); 689 FilteringPipeline pipeline = FilteringPipeline.getPipeline(); 690 pipeline.setOriginal(largeBitmap); 691 float previewScale = (float) largeBitmap.getWidth() / 692 (float) mImageLoader.getOriginalBounds().width(); 693 pipeline.setPreviewScaleFactor(previewScale); 694 Bitmap highresBitmap = mImageLoader.getOriginalBitmapHighres(); 695 if (highresBitmap != null) { 696 float highResPreviewScale = (float) highresBitmap.getWidth() / 697 (float) mImageLoader.getOriginalBounds().width(); 698 pipeline.setHighResPreviewScaleFactor(highResPreviewScale); 699 } 700 pipeline.turnOnPipeline(true); 701 MasterImage.getImage().setOriginalGeometry(largeBitmap); 702 } 703 } 704 705 private void rsPause() { 706 FilteringPipeline.getPipeline().turnOnPipeline(false); 707 FilteringPipeline.reset(); 708 ImageFilter.resetStatics(); 709 FiltersManager.getPreviewManager().freeRSFilterScripts(); 710 FiltersManager.getManager().freeRSFilterScripts(); 711 FiltersManager.getHighresManager().freeRSFilterScripts(); 712 FiltersManager.reset(); 713 CachingPipeline.destroyRenderScriptContext(); 714 } 715 716 @Override 717 public boolean onOptionsItemSelected(MenuItem item) { 718 switch (item.getItemId()) { 719 case R.id.undoButton: { 720 HistoryAdapter adapter = mMasterImage.getHistory(); 721 int position = adapter.undo(); 722 mMasterImage.onHistoryItemClick(position); 723 mImageShow.showToast("Undo"); 724 backToMain(); 725 invalidateViews(); 726 return true; 727 } 728 case R.id.redoButton: { 729 HistoryAdapter adapter = mMasterImage.getHistory(); 730 int position = adapter.redo(); 731 mMasterImage.onHistoryItemClick(position); 732 mImageShow.showToast("Redo"); 733 invalidateViews(); 734 return true; 735 } 736 case R.id.resetHistoryButton: { 737 resetHistory(); 738 return true; 739 } 740 case R.id.showImageStateButton: { 741 toggleImageStatePanel(); 742 return true; 743 } 744 case android.R.id.home: { 745 saveImage(); 746 return true; 747 } 748 } 749 return false; 750 } 751 752 public void enableSave(boolean enable) { 753 if (mSaveButton != null) 754 mSaveButton.setEnabled(enable); 755 } 756 757 private void fillFx() { 758 FilterFxRepresentation nullFx = 759 new FilterFxRepresentation(getString(R.string.none), 0, R.string.none); 760 Vector<FilterRepresentation> filtersRepresentations = new Vector<FilterRepresentation>(); 761 FiltersManager.getManager().addLooks(this, filtersRepresentations); 762 763 mCategoryLooksAdapter = new CategoryAdapter(this); 764 int verticalItemHeight = (int) getResources().getDimension(R.dimen.action_item_height); 765 mCategoryLooksAdapter.setItemHeight(verticalItemHeight); 766 mCategoryLooksAdapter.add(new Action(this, nullFx, Action.FULL_VIEW)); 767 for (FilterRepresentation representation : filtersRepresentations) { 768 mCategoryLooksAdapter.add(new Action(this, representation, Action.FULL_VIEW)); 769 } 770 } 771 772 public void setDefaultPreset() { 773 // Default preset (original) 774 ImagePreset preset = new ImagePreset(getString(R.string.history_original)); // empty 775 preset.setImageLoader(mImageLoader); 776 777 mMasterImage.setPreset(preset, true); 778 } 779 780 // ////////////////////////////////////////////////////////////////////////////// 781 // Some utility functions 782 // TODO: finish the cleanup. 783 784 public void invalidateViews() { 785 for (ImageShow views : mImageViews) { 786 views.invalidate(); 787 views.updateImage(); 788 } 789 } 790 791 public void hideImageViews() { 792 for (View view : mImageViews) { 793 view.setVisibility(View.GONE); 794 } 795 mEditorPlaceHolder.hide(); 796 } 797 798 // ////////////////////////////////////////////////////////////////////////////// 799 // imageState panel... 800 801 public void toggleImageStatePanel() { 802 invalidateOptionsMenu(); 803 mShowingImageStatePanel = !mShowingImageStatePanel; 804 Fragment panel = getSupportFragmentManager().findFragmentByTag(MainPanel.FRAGMENT_TAG); 805 if (panel != null) { 806 if (panel instanceof EditorPanel) { 807 EditorPanel editorPanel = (EditorPanel) panel; 808 editorPanel.showImageStatePanel(mShowingImageStatePanel); 809 } else if (panel instanceof MainPanel) { 810 MainPanel mainPanel = (MainPanel) panel; 811 mainPanel.showImageStatePanel(mShowingImageStatePanel); 812 } 813 } 814 } 815 816 @Override 817 public void onConfigurationChanged(Configuration newConfig) 818 { 819 super.onConfigurationChanged(newConfig); 820 setDefaultValues(); 821 loadXML(); 822 loadMainPanel(); 823 824 // mLoadBitmapTask==null implies you have looked at the intent 825 if (!mShowingTinyPlanet && (mLoadBitmapTask == null)) { 826 mCategoryFiltersAdapter.removeTinyPlanet(); 827 } 828 final View loading = findViewById(R.id.loading); 829 loading.setVisibility(View.GONE); 830 } 831 832 public void setupMasterImage() { 833 mImageLoader = new ImageLoader(this, getApplicationContext()); 834 835 HistoryAdapter mHistoryAdapter = new HistoryAdapter( 836 this, R.layout.filtershow_history_operation_row, 837 R.id.rowTextView); 838 839 StateAdapter mImageStateAdapter = new StateAdapter(this, 0); 840 MasterImage.reset(); 841 mMasterImage = MasterImage.getImage(); 842 mMasterImage.setHistoryAdapter(mHistoryAdapter); 843 mMasterImage.setStateAdapter(mImageStateAdapter); 844 mMasterImage.setActivity(this); 845 mMasterImage.setImageLoader(mImageLoader); 846 847 if (Runtime.getRuntime().maxMemory() > LIMIT_SUPPORTS_HIGHRES) { 848 mMasterImage.setSupportsHighRes(true); 849 } else { 850 mMasterImage.setSupportsHighRes(false); 851 } 852 } 853 854 void resetHistory() { 855 HistoryAdapter adapter = mMasterImage.getHistory(); 856 adapter.reset(); 857 ImagePreset original = new ImagePreset(adapter.getItem(0)); 858 mMasterImage.setPreset(original, true); 859 invalidateViews(); 860 backToMain(); 861 } 862 863 public void showDefaultImageView() { 864 mEditorPlaceHolder.hide(); 865 mImageShow.setVisibility(View.VISIBLE); 866 MasterImage.getImage().setCurrentFilter(null); 867 MasterImage.getImage().setCurrentFilterRepresentation(null); 868 } 869 870 public void backToMain() { 871 Fragment currentPanel = getSupportFragmentManager().findFragmentByTag(MainPanel.FRAGMENT_TAG); 872 if (currentPanel instanceof MainPanel) { 873 return; 874 } 875 loadMainPanel(); 876 showDefaultImageView(); 877 } 878 879 @Override 880 public void onBackPressed() { 881 Fragment currentPanel = getSupportFragmentManager().findFragmentByTag(MainPanel.FRAGMENT_TAG); 882 if (currentPanel instanceof MainPanel) { 883 if (!mImageShow.hasModifications()) { 884 done(); 885 } else { 886 AlertDialog.Builder builder = new AlertDialog.Builder(this); 887 builder.setMessage(R.string.unsaved).setTitle(R.string.save_before_exit); 888 builder.setPositiveButton(R.string.save_and_exit, new DialogInterface.OnClickListener() { 889 public void onClick(DialogInterface dialog, int id) { 890 saveImage(); 891 } 892 }); 893 builder.setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() { 894 public void onClick(DialogInterface dialog, int id) { 895 done(); 896 } 897 }); 898 builder.show(); 899 } 900 } else { 901 backToMain(); 902 } 903 } 904 905 public void cannotLoadImage() { 906 CharSequence text = getString(R.string.cannot_load_image); 907 Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT); 908 toast.show(); 909 finish(); 910 } 911 912 // ////////////////////////////////////////////////////////////////////////////// 913 914 public float getPixelsFromDip(float value) { 915 Resources r = getResources(); 916 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, 917 r.getDisplayMetrics()); 918 } 919 920 @Override 921 public void onItemClick(AdapterView<?> parent, View view, int position, 922 long id) { 923 mMasterImage.onHistoryItemClick(position); 924 invalidateViews(); 925 } 926 927 public void pickImage() { 928 Intent intent = new Intent(); 929 intent.setType("image/*"); 930 intent.setAction(Intent.ACTION_GET_CONTENT); 931 startActivityForResult(Intent.createChooser(intent, getString(R.string.select_image)), 932 SELECT_PICTURE); 933 } 934 935 @Override 936 public void onActivityResult(int requestCode, int resultCode, Intent data) { 937 if (resultCode == RESULT_OK) { 938 if (requestCode == SELECT_PICTURE) { 939 Uri selectedImageUri = data.getData(); 940 startLoadBitmap(selectedImageUri); 941 } 942 } 943 } 944 945 private boolean mSaveToExtraUri = false; 946 private boolean mSaveAsWallpaper = false; 947 private boolean mReturnAsExtra = false; 948 private boolean mOutputted = false; 949 950 public void saveImage() { 951 handleSpecialExitCases(); 952 if (!mOutputted) { 953 if (mImageShow.hasModifications()) { 954 // Get the name of the album, to which the image will be saved 955 File saveDir = SaveCopyTask.getFinalSaveDirectory(this, mImageLoader.getUri()); 956 int bucketId = GalleryUtils.getBucketId(saveDir.getPath()); 957 String albumName = LocalAlbum.getLocalizedName(getResources(), bucketId, null); 958 showSavingProgress(albumName); 959 mImageShow.saveImage(this, null); 960 } else { 961 done(); 962 } 963 } 964 } 965 966 public boolean detectSpecialExitCases() { 967 return mCropExtras != null && (mCropExtras.getExtraOutput() != null 968 || mCropExtras.getSetAsWallpaper() || mCropExtras.getReturnData()); 969 } 970 971 public void handleSpecialExitCases() { 972 if (mCropExtras != null) { 973 if (mCropExtras.getExtraOutput() != null) { 974 mSaveToExtraUri = true; 975 mOutputted = true; 976 } 977 if (mCropExtras.getSetAsWallpaper()) { 978 mSaveAsWallpaper = true; 979 mOutputted = true; 980 } 981 if (mCropExtras.getReturnData()) { 982 mReturnAsExtra = true; 983 mOutputted = true; 984 } 985 if (mOutputted) { 986 mImageShow.getImagePreset().mGeoData.setUseCropExtrasFlag(true); 987 showSavingProgress(null); 988 mImageShow.returnFilteredResult(this); 989 } 990 } 991 } 992 993 public void onFilteredResult(Bitmap filtered) { 994 Intent intent = new Intent(); 995 intent.putExtra(CropExtras.KEY_CROPPED_RECT, mImageShow.getImageCropBounds()); 996 if (mSaveToExtraUri) { 997 mImageShow.saveToUri(filtered, mCropExtras.getExtraOutput(), 998 mCropExtras.getOutputFormat(), this); 999 } 1000 if (mSaveAsWallpaper) { 1001 setWallpaperInBackground(filtered); 1002 } 1003 if (mReturnAsExtra) { 1004 if (filtered != null) { 1005 int bmapSize = filtered.getRowBytes() * filtered.getHeight(); 1006 /* 1007 * Max size of Binder transaction buffer is 1Mb, so constrain 1008 * Bitmap to be somewhat less than this, otherwise we get 1009 * TransactionTooLargeExceptions. 1010 */ 1011 if (bmapSize > MAX_BMAP_IN_INTENT) { 1012 Log.w(LOGTAG, "Bitmap too large to be returned via intent"); 1013 } else { 1014 intent.putExtra(CropExtras.KEY_DATA, filtered); 1015 } 1016 } 1017 } 1018 setResult(RESULT_OK, intent); 1019 if (!mSaveToExtraUri) { 1020 done(); 1021 } 1022 } 1023 1024 void setWallpaperInBackground(final Bitmap bmap) { 1025 Toast.makeText(this, R.string.setting_wallpaper, Toast.LENGTH_LONG).show(); 1026 BitmapTask.Callbacks<FilterShowActivity> cb = new BitmapTask.Callbacks<FilterShowActivity>() { 1027 @Override 1028 public void onComplete(Bitmap result) {} 1029 1030 @Override 1031 public void onCancel() {} 1032 1033 @Override 1034 public Bitmap onExecute(FilterShowActivity param) { 1035 try { 1036 WallpaperManager.getInstance(param).setBitmap(bmap); 1037 } catch (IOException e) { 1038 Log.w(LOGTAG, "fail to set wall paper", e); 1039 } 1040 return null; 1041 } 1042 }; 1043 (new BitmapTask<FilterShowActivity>(cb)).execute(this); 1044 } 1045 1046 public void done() { 1047 if (mOutputted) { 1048 hideSavingProgress(); 1049 } 1050 finish(); 1051 } 1052 1053 static { 1054 System.loadLibrary("jni_filtershow_filters"); 1055 } 1056 1057 } 1058