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