Statistics
| Branch: | Tag: | Revision:

securesmartcam / app / src / main / java / org / witness / obscuracam / ObscuraApp.java @ 41590feb

History | View | Annotate | Download (9.06 KB)

1
package org.witness.obscuracam;
2

    
3

    
4

    
5
import java.io.File;
6

    
7
import org.witness.obscuracam.ui.AlbumsActivity;
8
import org.witness.obscuracam.ui.ImageEditor;
9
import org.witness.obscuracam.video.VideoEditor;
10
import org.witness.sscphase1.R;
11

    
12
import android.app.AlertDialog;
13
import android.content.ContentValues;
14
import android.content.Intent;
15
import android.content.res.Configuration;
16
import android.database.Cursor;
17
import android.net.Uri;
18
import android.os.Bundle;
19
import android.os.Environment;
20
import android.provider.MediaStore;
21
import android.support.v7.app.AppCompatActivity;
22
import android.util.Log;
23
import android.view.Menu;
24
import android.view.MenuItem;
25
import android.view.View;
26
import android.view.View.OnClickListener;
27
import android.view.Window;
28
import android.widget.Button;
29
import android.widget.Toast;
30

    
31
public class ObscuraApp extends AppCompatActivity implements OnClickListener {
32
            
33
        public final static String TAG = "SSC";
34
                
35
        final static int CAMERA_RESULT = 0;
36
        final static int GALLERY_RESULT = 1;
37
        final static int IMAGE_EDITOR = 2;
38
        final static int VIDEO_EDITOR = 3;
39
        final static int ABOUT = 0;
40
        
41
        final static String CAMERA_TMP_FILE = "ssctmp.jpg";
42
        
43
        private Button choosePictureButton, chooseVideoButton, takePictureButton;                
44
        
45
        private Uri uriCameraImage = null;
46
        
47
        @Override
48
        protected void onDestroy() 
49
        {
50
                super.onDestroy();        
51
                deleteTmpFile();
52
                
53
        }
54
        
55
        private void deleteTmpFile ()
56
        {
57
                File fileDir = getExternalFilesDir(null);
58
                
59
                if (fileDir == null || !fileDir.exists())
60
                        fileDir = getFilesDir();
61
                
62
                File tmpFile = new File(fileDir,CAMERA_TMP_FILE);
63
                if (tmpFile.exists())
64
                        tmpFile.delete();
65
        }
66

    
67

    
68
                                        
69
    @Override
70
    public void onCreate(Bundle savedInstanceState) {
71
        super.onCreate(savedInstanceState);
72

    
73
        requestWindowFeature(Window.FEATURE_NO_TITLE);
74
        
75
        setLayout();
76
        deleteTmpFile();
77

    
78
    }
79
    
80
    @Override
81
        protected void onResume() {
82

    
83
                super.onResume();
84

    
85
        
86
        }
87

    
88
        private void setLayout() {
89
                
90
        setContentView(R.layout.mainmenu);
91

    
92
                choosePictureButton = (Button) this.findViewById(R.id.ChoosePictureButton);
93
            choosePictureButton.setOnClickListener(this);
94
            
95
            chooseVideoButton = (Button) this.findViewById(R.id.ChooseVideoButton);
96
            chooseVideoButton.setOnClickListener(this);
97
            
98
            takePictureButton = (Button) this.findViewById(R.id.TakePictureButton);
99
            takePictureButton.setOnClickListener(this);
100
                
101
    }
102

    
103
        public void onClick(View v) {
104
                if (v == choosePictureButton) 
105
                {
106
                        Intent intentAlbums = new Intent(this, AlbumsActivity.class);
107
                        startActivityForResult(intentAlbums, 4711); //SELECT_FROM_ALBUMS_REQUEST);
108
//                        try
109
//                        {
110
//                                 setContentView(R.layout.mainloading);
111
//                                Intent intent = new Intent(Intent.ACTION_PICK);
112
//                                intent.setType("image/*"); //limit to image types for now
113
//                                startActivityForResult(intent, GALLERY_RESULT);
114
//
115
//                        }
116
//                        catch (Exception e)
117
//                        {
118
//                                Toast.makeText(this, "Unable to open Gallery app", Toast.LENGTH_LONG).show();
119
//                                Log.e(TAG, "error loading gallery app to choose photo: " + e.getMessage(), e);
120
//                        }
121
                        
122
                } 
123
                else if (v == chooseVideoButton) 
124
                {
125
                        
126
                        try
127
                        {
128
                                 setContentView(R.layout.mainloading);
129
                                Intent intent = new Intent(Intent.ACTION_PICK);
130
                                intent.setType("video/*"); //limit to image types for now
131
                                startActivityForResult(intent, GALLERY_RESULT);
132
                                
133
                        }
134
                        catch (Exception e)
135
                        {
136
                                Toast.makeText(this, "Unable to open Gallery app", Toast.LENGTH_LONG).show();
137
                                Log.e(TAG, "error loading gallery app to choose photo: " + e.getMessage(), e);
138
                        }
139
                        
140
                }
141
                else if (v == takePictureButton) {
142
                        
143
                        setContentView(R.layout.mainloading);
144
                        
145
                        String storageState = Environment.getExternalStorageState();
146
                if(storageState.equals(Environment.MEDIA_MOUNTED)) {
147

    
148
                  
149
                    ContentValues values = new ContentValues();
150
                  
151
                    values.put(MediaStore.Images.Media.TITLE, CAMERA_TMP_FILE);
152
              
153
                    values.put(MediaStore.Images.Media.DESCRIPTION,"ssctmp");
154

    
155
                    File tmpFileDirectory = new File(Environment.getExternalStorageDirectory().getPath() + ImageEditor.TMP_FILE_DIRECTORY);
156
                    if (!tmpFileDirectory.exists())
157
                            tmpFileDirectory.mkdirs();
158
                    
159
                    File tmpFile = new File(tmpFileDirectory,"cam" + ImageEditor.TMP_FILE_NAME);
160
                        
161
                        uriCameraImage = Uri.fromFile(tmpFile);
162
                    //uriCameraImage = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
163

    
164
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE );
165
                    intent.putExtra( MediaStore.EXTRA_OUTPUT, uriCameraImage);
166
                    
167
                    startActivityForResult(intent, CAMERA_RESULT);
168
                }   else {
169
                    new AlertDialog.Builder(ObscuraApp.this)
170
                    .setMessage("External Storeage (SD Card) is required.\n\nCurrent state: " + storageState)
171
                    .setCancelable(true).create().show();
172
                }
173
                
174
                        takePictureButton.setVisibility(View.VISIBLE);
175
                        choosePictureButton.setVisibility(View.VISIBLE);
176
                        chooseVideoButton.setVisibility(View.VISIBLE);
177
                        
178
                } 
179
        }
180

    
181
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
182
                
183
                
184
                if (resultCode == RESULT_OK)
185
                {
186
                        setContentView(R.layout.mainloading);
187
                        
188
                        if (requestCode == GALLERY_RESULT) 
189
                        {
190
                                if (intent != null)
191
                                {
192
                                        Uri uriGalleryFile = intent.getData();
193
                                        
194
                                        try
195
                                                {
196
                                                        if (uriGalleryFile != null)
197
                                                        {
198
                                                                Cursor cursor = managedQuery(uriGalleryFile, null, 
199
                                                null, null, null); 
200
                                                                cursor.moveToNext(); 
201
                                                                // Retrieve the path and the mime type 
202
                                                                String path = cursor.getString(cursor 
203
                                                                                .getColumnIndex(MediaStore.MediaColumns.DATA)); 
204
                                                                String mimeType = cursor.getString(cursor 
205
                                                                                .getColumnIndex(MediaStore.MediaColumns.MIME_TYPE));
206
                                                                
207
                                                                if (mimeType == null || mimeType.startsWith("image"))
208
                                                                {
209
                                                                        Intent passingIntent = new Intent(this,ImageEditor.class);
210
                                                                        passingIntent.setData(uriGalleryFile);
211
                                                                        startActivityForResult(passingIntent,IMAGE_EDITOR);
212
                                                                }
213
                                                                else if (mimeType.startsWith("video"))
214
                                                                {
215
                
216
                                                                        Intent passingIntent = new Intent(this,VideoEditor.class);
217
                                                                        passingIntent.setData(uriGalleryFile);
218
                                                                        startActivityForResult(passingIntent,VIDEO_EDITOR);
219
                                                                }
220
                                                        }
221
                                                        else
222
                                                        {
223
                                                                Toast.makeText(this, "Unable to load media.", Toast.LENGTH_LONG).show();
224
                        
225
                                                        }
226
                                                }
227
                                        catch (Exception e)
228
                                        {
229
                                                Toast.makeText(this, "Unable to load media.", Toast.LENGTH_LONG).show();
230
                                                Log.e(TAG, "error loading media: " + e.getMessage(), e);
231

    
232
                                        }
233
                                }
234
                                else
235
                                {
236
                                        Toast.makeText(this, "Unable to load photo.", Toast.LENGTH_LONG).show();
237
        
238
                                }
239
                                        
240
                        }
241
                        else if (requestCode == CAMERA_RESULT)
242
                        {
243
                                //Uri uriCameraImage = intent.getData();
244
                                
245
                                if (uriCameraImage != null)
246
                                {
247
                                        Intent passingIntent = new Intent(this,ImageEditor.class);
248
                                        passingIntent.setData(uriCameraImage);
249
                                        startActivityForResult(passingIntent,IMAGE_EDITOR);
250
                                }
251
                                else
252
                                {
253
                                        takePictureButton.setVisibility(View.VISIBLE);
254
                                        choosePictureButton.setVisibility(View.VISIBLE);
255
                                }
256
                        }
257
                }
258
                else
259
                        setLayout();
260
                
261
                
262
                
263
        }        
264

    
265
        /*
266
         * Display the about screen
267
         */
268
        private void displayAbout() {
269
                
270
                StringBuffer msg = new StringBuffer();
271
                
272
                msg.append(getString(R.string.app_name));
273
                
274
        String versNum = "";
275
        
276
        try {
277
            String pkg = getPackageName();
278
            versNum = getPackageManager().getPackageInfo(pkg, 0).versionName;
279
        } catch (Exception e) {
280
                versNum = "";
281
        }
282
        
283
        msg.append(" v" + versNum);
284
        msg.append('\n');
285
        msg.append('\n');
286
        
287
        msg.append(getString(R.string.about));
288
                
289
        msg.append('\n');
290
        msg.append('\n');
291
        
292
        msg.append(getString(R.string.about2));
293
        
294
        msg.append('\n');
295
        msg.append('\n');
296
        
297
        msg.append(getString(R.string.about3));
298
        
299
                showDialog(msg.toString());
300
        }
301
        
302
        private void showDialog (String msg)
303
        {
304
                 new AlertDialog.Builder(this)
305
         .setTitle(getString(R.string.app_name))
306
         .setMessage(msg)
307
         .create().show();
308
        }
309

    
310

    
311
        @Override
312
    public boolean onCreateOptionsMenu(Menu menu) {
313
                
314
                String aboutString = "About ObscuraCam";
315
                
316
            MenuItem aboutMenuItem = menu.add(Menu.NONE, ABOUT, Menu.NONE, aboutString);
317
            aboutMenuItem.setIcon(R.drawable.ic_menu_about);
318
            
319
            
320
            return true;
321
        }
322
        
323
    public boolean onOptionsItemSelected(MenuItem item) {        
324
        switch (item.getItemId()) {
325
                case ABOUT:
326
                        displayAbout();
327
                        return true;
328
                        
329
                default:
330
                        
331
                        return false;
332
        }
333
    }
334
    
335
        
336
    /*
337
     * Handling screen configuration changes ourselves, 
338
     * we don't want the activity to restart on rotation
339
     */
340
    @Override
341
    public void onConfigurationChanged(Configuration conf) 
342
    {
343
        super.onConfigurationChanged(conf);
344
        // Reset the layout to use the landscape config
345
        setLayout();
346
    }
347

    
348

    
349
}