1 /* 2 * Copyright (C) 2010 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.photoeditor; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 22 import com.android.photoeditor.filters.ImageUtils; 23 24 import android.app.Activity; 25 import android.app.AlertDialog; 26 import android.content.DialogInterface; 27 import android.content.Intent; 28 import android.content.res.Resources; 29 import android.os.Bundle; 30 import android.view.View; 31 32 /** 33 * Main activity of the photo editor. 34 */ 35 public class PhotoEditor extends Activity { 36 37 private Toolbar toolbar; 38 private View backButton; 39 40 @Override 41 public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.main); 44 45 if (ImageUtils.gdk()) 46 { 47 // HACK: create faked view in order to read bitcode in resource 48 View view = new View(getApplication()); 49 byte[] pgm; 50 int pgmLength; 51 52 // read bitcode in res 53 InputStream is = view.getResources().openRawResource(R.raw.libjni_photoeditor_portable); 54 try { 55 try { 56 pgm = new byte[1024]; 57 pgmLength = 0; 58 59 while(true) { 60 int bytesLeft = pgm.length - pgmLength; 61 if (bytesLeft == 0) { 62 byte[] buf2 = new byte[pgm.length * 2]; 63 System.arraycopy(pgm, 0, buf2, 0, pgm.length); 64 pgm = buf2; 65 bytesLeft = pgm.length - pgmLength; 66 } 67 int bytesRead = is.read(pgm, pgmLength, bytesLeft); 68 if (bytesRead <= 0) { 69 break; 70 } 71 pgmLength += bytesRead; 72 } 73 ImageUtils.init(pgm, pgmLength); 74 } finally { 75 is.close(); 76 } 77 } catch(IOException e) { 78 throw new Resources.NotFoundException(); 79 } 80 } 81 82 toolbar = (Toolbar) findViewById(R.id.toolbar); 83 toolbar.initialize(); 84 85 final EffectsBar effectsBar = (EffectsBar) findViewById(R.id.effects_bar); 86 final View actionBar = findViewById(R.id.action_bar); 87 final View quickviewOn = findViewById(R.id.quickview_on_button); 88 backButton = findViewById(R.id.action_bar_back); 89 backButton.setOnClickListener(new View.OnClickListener() { 90 91 @Override 92 public void onClick(View v) { 93 if (actionBar.isEnabled()) { 94 if (quickviewOn.getVisibility() == View.VISIBLE) { 95 quickviewOn.performClick(); 96 } else if (effectsBar.hasEffectOn()) { 97 effectsBar.effectsOff(null); 98 } else { 99 tryRun(new Runnable() { 100 101 @Override 102 public void run() { 103 finish(); 104 } 105 }); 106 } 107 } 108 } 109 }); 110 111 Intent intent = getIntent(); 112 if (Intent.ACTION_EDIT.equalsIgnoreCase(intent.getAction()) && (intent.getData() != null)) { 113 toolbar.openPhoto(intent.getData()); 114 } 115 } 116 117 private void tryRun(final Runnable runnable) { 118 if (findViewById(R.id.save_button).isEnabled()) { 119 // Pop-up a dialog before executing the runnable to save unsaved photo. 120 AlertDialog.Builder builder = new AlertDialog.Builder(this) 121 .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 122 123 @Override 124 public void onClick(DialogInterface dialog, int which) { 125 toolbar.savePhoto(new Runnable() { 126 127 @Override 128 public void run() { 129 runnable.run(); 130 } 131 }); 132 } 133 }) 134 .setNeutralButton(R.string.no, new DialogInterface.OnClickListener() { 135 136 @Override 137 public void onClick(DialogInterface dialog, int which) { 138 runnable.run(); 139 } 140 }) 141 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 142 143 @Override 144 public void onClick(DialogInterface dialog, int which) { 145 // no-op 146 } 147 }); 148 builder.setMessage(R.string.save_photo).show(); 149 return; 150 } 151 152 runnable.run(); 153 } 154 155 @Override 156 public void onBackPressed() { 157 backButton.performClick(); 158 } 159 } 160