securesmartcam / app / src / main / java / org / witness / obscuracam / MainActivity.java @ 41590feb
History | View | Annotate | Download (13 KB)
| 1 |
package org.witness.obscuracam; |
|---|---|
| 2 |
|
| 3 |
|
| 4 |
import android.Manifest; |
| 5 |
import android.app.AlertDialog; |
| 6 |
import android.content.ClipData; |
| 7 |
import android.content.Intent; |
| 8 |
import android.content.pm.PackageManager; |
| 9 |
import android.content.pm.ResolveInfo; |
| 10 |
import android.content.res.Configuration; |
| 11 |
import android.net.Uri; |
| 12 |
import android.os.Build; |
| 13 |
import android.os.Bundle; |
| 14 |
import android.provider.MediaStore; |
| 15 |
import android.support.v4.app.ActivityCompat; |
| 16 |
import android.support.v4.content.ContextCompat; |
| 17 |
import android.support.v4.content.FileProvider; |
| 18 |
import android.support.v7.app.AppCompatActivity; |
| 19 |
import android.support.v7.widget.LinearLayoutManager; |
| 20 |
import android.support.v7.widget.RecyclerView; |
| 21 |
import android.support.v7.widget.Toolbar; |
| 22 |
import android.view.Menu; |
| 23 |
import android.view.MenuItem; |
| 24 |
import android.view.View; |
| 25 |
import android.widget.Button; |
| 26 |
|
| 27 |
import org.witness.obscuracam.ui.AlbumLayoutManager; |
| 28 |
import org.witness.obscuracam.ui.AlbumsActivity; |
| 29 |
import org.witness.obscuracam.ui.ImageEditor; |
| 30 |
import org.witness.obscuracam.ui.adapters.AskForPermissionAdapter; |
| 31 |
import org.witness.obscuracam.ui.adapters.GalleryCursorRecyclerViewAdapter; |
| 32 |
import org.witness.obscuracam.video.VideoEditor; |
| 33 |
import org.witness.sscphase1.R; |
| 34 |
|
| 35 |
import java.io.File; |
| 36 |
import java.io.IOException; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
public class MainActivity extends AppCompatActivity implements GalleryCursorRecyclerViewAdapter.GalleryCursorRecyclerViewAdapterListener { |
| 40 |
|
| 41 |
public final static String TAG = "SSC"; |
| 42 |
|
| 43 |
private static final int READ_EXTERNAL_STORAGE_PERMISSION_REQUEST = 1; |
| 44 |
private static final int CAPTURE_IMAGE_REQUEST = 2; |
| 45 |
private static final int SELECT_FROM_ALBUMS_REQUEST = 3; |
| 46 |
|
| 47 |
private static final String CAMERA_CAPTURE_DIRNAME = "camera"; |
| 48 |
private static final String CAMERA_CAPTURE_FILENAME = "cameracapture"; |
| 49 |
|
| 50 |
final static int CAMERA_RESULT = 0; |
| 51 |
final static int GALLERY_RESULT = 1; |
| 52 |
final static int IMAGE_EDITOR = 2; |
| 53 |
final static int VIDEO_EDITOR = 3; |
| 54 |
final static int ABOUT = 0; |
| 55 |
|
| 56 |
private RecyclerView recyclerViewPhotos;
|
| 57 |
private View layoutGalleryInfo; |
| 58 |
private AlbumLayoutManager layoutManagerPhotos;
|
| 59 |
|
| 60 |
@Override
|
| 61 |
protected void onDestroy() |
| 62 |
{
|
| 63 |
super.onDestroy();
|
| 64 |
deleteTmpFile(); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
@Override
|
| 69 |
public void onCreate(Bundle savedInstanceState) { |
| 70 |
super.onCreate(savedInstanceState);
|
| 71 |
setContentView(R.layout.activity_main); |
| 72 |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 73 |
setSupportActionBar(toolbar); |
| 74 |
deleteTmpFile(); |
| 75 |
|
| 76 |
layoutGalleryInfo = findViewById(R.id.gallery_info); |
| 77 |
Button btnOk = (Button) layoutGalleryInfo.findViewById(R.id.btnGalleryInfoOk); |
| 78 |
btnOk.setOnClickListener(new View.OnClickListener() { |
| 79 |
@Override
|
| 80 |
public void onClick(View view) { |
| 81 |
//TODO App.getInstance().getSettings().setSkipGalleryInfo(true);
|
| 82 |
layoutGalleryInfo.setVisibility(View.GONE);
|
| 83 |
} |
| 84 |
}); |
| 85 |
layoutGalleryInfo.setVisibility(View.GONE);
|
| 86 |
|
| 87 |
recyclerViewPhotos = (RecyclerView) findViewById(R.id.recycler_view_albums); |
| 88 |
int colWidth = getResources().getDimensionPixelSize(R.dimen.photo_column_size);
|
| 89 |
layoutManagerPhotos = new AlbumLayoutManager(this, colWidth); |
| 90 |
recyclerViewPhotos.setLayoutManager(layoutManagerPhotos); |
| 91 |
|
| 92 |
setCurrentMode(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override
|
| 96 |
protected void onResume() { |
| 97 |
|
| 98 |
super.onResume();
|
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
private void setLayout() { |
| 103 |
setContentView(R.layout.activity_main); |
| 104 |
recyclerViewPhotos = (RecyclerView) findViewById(R.id.recycler_view_albums); |
| 105 |
} |
| 106 |
|
| 107 |
/* protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
| 108 |
|
| 109 |
|
| 110 |
if (resultCode == RESULT_OK)
|
| 111 |
{
|
| 112 |
setContentView(R.layout.mainloading);
|
| 113 |
|
| 114 |
if (requestCode == GALLERY_RESULT)
|
| 115 |
{
|
| 116 |
if (intent != null)
|
| 117 |
{
|
| 118 |
Uri uriGalleryFile = intent.getData();
|
| 119 |
|
| 120 |
try
|
| 121 |
{
|
| 122 |
if (uriGalleryFile != null)
|
| 123 |
{
|
| 124 |
Cursor cursor = managedQuery(uriGalleryFile, null,
|
| 125 |
null, null, null);
|
| 126 |
cursor.moveToNext();
|
| 127 |
// Retrieve the path and the mime type
|
| 128 |
String path = cursor.getString(cursor
|
| 129 |
.getColumnIndex(MediaStore.MediaColumns.DATA));
|
| 130 |
String mimeType = cursor.getString(cursor
|
| 131 |
.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE));
|
| 132 |
|
| 133 |
if (mimeType == null || mimeType.startsWith("image"))
|
| 134 |
{
|
| 135 |
Intent passingIntent = new Intent(this,ImageEditor.class);
|
| 136 |
passingIntent.setData(uriGalleryFile);
|
| 137 |
startActivityForResult(passingIntent,IMAGE_EDITOR);
|
| 138 |
}
|
| 139 |
else if (mimeType.startsWith("video"))
|
| 140 |
{
|
| 141 |
|
| 142 |
Intent passingIntent = new Intent(this,VideoEditor.class);
|
| 143 |
passingIntent.setData(uriGalleryFile);
|
| 144 |
startActivityForResult(passingIntent,VIDEO_EDITOR);
|
| 145 |
}
|
| 146 |
}
|
| 147 |
else
|
| 148 |
{
|
| 149 |
Toast.makeText(this, "Unable to load media.", Toast.LENGTH_LONG).show();
|
| 150 |
|
| 151 |
}
|
| 152 |
}
|
| 153 |
catch (Exception e)
|
| 154 |
{
|
| 155 |
Toast.makeText(this, "Unable to load media.", Toast.LENGTH_LONG).show();
|
| 156 |
Log.e(TAG, "error loading media: " + e.getMessage(), e);
|
| 157 |
|
| 158 |
}
|
| 159 |
}
|
| 160 |
else
|
| 161 |
{
|
| 162 |
Toast.makeText(this, "Unable to load photo.", Toast.LENGTH_LONG).show();
|
| 163 |
|
| 164 |
}
|
| 165 |
|
| 166 |
}
|
| 167 |
else if (requestCode == CAMERA_RESULT)
|
| 168 |
{
|
| 169 |
//Uri uriCameraImage = intent.getData();
|
| 170 |
|
| 171 |
if (uriCameraImage != null)
|
| 172 |
{
|
| 173 |
Intent passingIntent = new Intent(this,ImageEditor.class);
|
| 174 |
passingIntent.setData(uriCameraImage);
|
| 175 |
startActivityForResult(passingIntent,IMAGE_EDITOR);
|
| 176 |
}
|
| 177 |
else
|
| 178 |
{
|
| 179 |
takePictureButton.setVisibility(View.VISIBLE);
|
| 180 |
choosePictureButton.setVisibility(View.VISIBLE);
|
| 181 |
}
|
| 182 |
}
|
| 183 |
}
|
| 184 |
else
|
| 185 |
setLayout();
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
} */
|
| 190 |
|
| 191 |
/*
|
| 192 |
* Display the about screen
|
| 193 |
*/
|
| 194 |
private void displayAbout() { |
| 195 |
|
| 196 |
StringBuffer msg = new StringBuffer(); |
| 197 |
|
| 198 |
msg.append(getString(R.string.app_name)); |
| 199 |
|
| 200 |
String versNum = ""; |
| 201 |
|
| 202 |
try {
|
| 203 |
String pkg = getPackageName();
|
| 204 |
versNum = getPackageManager().getPackageInfo(pkg, 0).versionName;
|
| 205 |
} catch (Exception e) { |
| 206 |
versNum = "";
|
| 207 |
} |
| 208 |
|
| 209 |
msg.append(" v" + versNum);
|
| 210 |
msg.append('\n');
|
| 211 |
msg.append('\n');
|
| 212 |
|
| 213 |
msg.append(getString(R.string.about)); |
| 214 |
|
| 215 |
msg.append('\n');
|
| 216 |
msg.append('\n');
|
| 217 |
|
| 218 |
msg.append(getString(R.string.about2)); |
| 219 |
|
| 220 |
msg.append('\n');
|
| 221 |
msg.append('\n');
|
| 222 |
|
| 223 |
msg.append(getString(R.string.about3)); |
| 224 |
|
| 225 |
showDialog(msg.toString()); |
| 226 |
} |
| 227 |
|
| 228 |
private void showDialog (String msg) |
| 229 |
{
|
| 230 |
new AlertDialog.Builder(this) |
| 231 |
.setTitle(getString(R.string.app_name)) |
| 232 |
.setMessage(msg) |
| 233 |
.create().show(); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
@Override
|
| 238 |
public boolean onCreateOptionsMenu(Menu menu) { |
| 239 |
|
| 240 |
String aboutString = "About ObscuraCam"; |
| 241 |
|
| 242 |
MenuItem aboutMenuItem = menu.add(Menu.NONE, ABOUT, Menu.NONE, aboutString); |
| 243 |
aboutMenuItem.setIcon(R.drawable.ic_menu_about); |
| 244 |
|
| 245 |
|
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
public boolean onOptionsItemSelected(MenuItem item) { |
| 250 |
switch (item.getItemId()) {
|
| 251 |
case ABOUT:
|
| 252 |
displayAbout(); |
| 253 |
return true; |
| 254 |
|
| 255 |
default:
|
| 256 |
|
| 257 |
return false; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/*
|
| 263 |
* Handling screen configuration changes ourselves,
|
| 264 |
* we don't want the activity to restart on rotation
|
| 265 |
*/
|
| 266 |
@Override
|
| 267 |
public void onConfigurationChanged(Configuration conf) |
| 268 |
{
|
| 269 |
super.onConfigurationChanged(conf);
|
| 270 |
// Reset the layout to use the landscape config
|
| 271 |
setLayout(); |
| 272 |
} |
| 273 |
|
| 274 |
private void setCurrentMode() { |
| 275 |
int permissionCheck = ContextCompat.checkSelfPermission(this, |
| 276 |
Manifest.permission.READ_EXTERNAL_STORAGE);
|
| 277 |
if (Build.VERSION.SDK_INT <= 18) |
| 278 |
permissionCheck = PackageManager.PERMISSION_GRANTED; // For old devices we ask in the manifest!
|
| 279 |
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
|
| 280 |
AskForPermissionAdapter adapter = new AskForPermissionAdapter(this); |
| 281 |
recyclerViewPhotos.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); |
| 282 |
recyclerViewPhotos.setAdapter(adapter); |
| 283 |
} else {
|
| 284 |
applyCurrentMode(); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
public void askForReadExternalStoragePermission() { |
| 289 |
ActivityCompat.requestPermissions(this, new String[]{ |
| 290 |
Manifest.permission.READ_EXTERNAL_STORAGE,
|
| 291 |
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
| 292 |
}, |
| 293 |
READ_EXTERNAL_STORAGE_PERMISSION_REQUEST); |
| 294 |
} |
| 295 |
|
| 296 |
@Override
|
| 297 |
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { |
| 298 |
switch (requestCode) {
|
| 299 |
case READ_EXTERNAL_STORAGE_PERMISSION_REQUEST: {
|
| 300 |
// If request is cancelled, the result arrays are empty.
|
| 301 |
if (grantResults.length > 1 |
| 302 |
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
|
| 303 |
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
|
| 304 |
applyCurrentMode(); |
| 305 |
} |
| 306 |
} |
| 307 |
break;
|
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
private void applyCurrentMode() { |
| 312 |
//TODO layoutGalleryInfo.setVisibility(App.getInstance().getSettings().skipGalleryInfo() ? View.GONE : View.VISIBLE);
|
| 313 |
setPhotosAdapter(null, true, true); |
| 314 |
} |
| 315 |
|
| 316 |
private void setPhotosAdapter(String album, boolean showCamera, boolean showAlbums) { |
| 317 |
recyclerViewPhotos.setLayoutManager(layoutManagerPhotos); |
| 318 |
GalleryCursorRecyclerViewAdapter adapter = new GalleryCursorRecyclerViewAdapter(this, album, showCamera, showAlbums); |
| 319 |
adapter.setListener(this);
|
| 320 |
int colWidth = getResources().getDimensionPixelSize(R.dimen.photo_column_size);
|
| 321 |
layoutManagerPhotos.setColumnWidth(colWidth); |
| 322 |
recyclerViewPhotos.setAdapter(adapter); |
| 323 |
} |
| 324 |
|
| 325 |
@Override
|
| 326 |
public void onPhotoSelected(String photo, View thumbView) { |
| 327 |
final Uri uri = Uri.parse(photo);
|
| 328 |
if (uri != null) { |
| 329 |
try {
|
| 330 |
Intent passingIntent = new Intent(this, ImageEditor.class); |
| 331 |
passingIntent.setData(uri); |
| 332 |
startActivityForResult(passingIntent, IMAGE_EDITOR); |
| 333 |
} catch (Exception e) { |
| 334 |
e.printStackTrace(); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
@Override
|
| 340 |
public void onVideoSelected(String photo, View thumbView) { |
| 341 |
final Uri uri = Uri.parse(photo);
|
| 342 |
if (uri != null) { |
| 343 |
try {
|
| 344 |
Intent passingIntent = new Intent(this, VideoEditor.class); |
| 345 |
passingIntent.setData(uri); |
| 346 |
startActivityForResult(passingIntent, VIDEO_EDITOR); |
| 347 |
} catch (Exception e) { |
| 348 |
e.printStackTrace(); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
@Override
|
| 354 |
public void onCameraSelected() { |
| 355 |
takePicture(); |
| 356 |
} |
| 357 |
|
| 358 |
@Override
|
| 359 |
public void onAlbumsSelected() { |
| 360 |
Intent intentAlbums = new Intent(this, AlbumsActivity.class); |
| 361 |
startActivityForResult(intentAlbums, SELECT_FROM_ALBUMS_REQUEST); |
| 362 |
} |
| 363 |
|
| 364 |
private void takePicture() { |
| 365 |
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
| 366 |
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { |
| 367 |
|
| 368 |
try {
|
| 369 |
File storageDir = new File(getCacheDir(), CAMERA_CAPTURE_DIRNAME); |
| 370 |
if (!storageDir.exists()) {
|
| 371 |
storageDir.mkdir(); |
| 372 |
} |
| 373 |
File image = new File(storageDir, CAMERA_CAPTURE_FILENAME); |
| 374 |
if (image.exists()) {
|
| 375 |
image.delete(); |
| 376 |
} |
| 377 |
image.createNewFile(); |
| 378 |
|
| 379 |
Uri photoURI = FileProvider.getUriForFile(this,
|
| 380 |
"org.witness.securesmartcam.camera_capture",
|
| 381 |
image); |
| 382 |
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); |
| 383 |
|
| 384 |
// Need to grant uri permissions, see here:
|
| 385 |
// http://stackoverflow.com/questions/33650632/fileprovider-not-working-with-camera
|
| 386 |
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
|
| 387 |
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 388 |
} |
| 389 |
else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) { |
| 390 |
ClipData clip= |
| 391 |
ClipData.newUri(getContentResolver(), "A photo", photoURI);
|
| 392 |
|
| 393 |
takePictureIntent.setClipData(clip); |
| 394 |
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 395 |
} |
| 396 |
else {
|
| 397 |
List<ResolveInfo> resInfoList=
|
| 398 |
getPackageManager() |
| 399 |
.queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY); |
| 400 |
|
| 401 |
for (ResolveInfo resolveInfo : resInfoList) {
|
| 402 |
String packageName = resolveInfo.activityInfo.packageName;
|
| 403 |
grantUriPermission(packageName, photoURI, |
| 404 |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_REQUEST); |
| 409 |
} catch (IOException ignored) { |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
@Override
|
| 415 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 416 |
if (requestCode == CAPTURE_IMAGE_REQUEST && resultCode == RESULT_OK) {
|
| 417 |
try {
|
| 418 |
File storageDir = new File(getCacheDir(), CAMERA_CAPTURE_DIRNAME); |
| 419 |
File image = new File(storageDir, CAMERA_CAPTURE_FILENAME); |
| 420 |
if (image.exists()) {
|
| 421 |
onPhotoSelected(image.getAbsolutePath(), null);
|
| 422 |
//TODO image.delete();
|
| 423 |
} |
| 424 |
} catch (Exception ignored) { |
| 425 |
} |
| 426 |
} else if (requestCode == SELECT_FROM_ALBUMS_REQUEST) { |
| 427 |
if (resultCode == RESULT_OK && data != null && data.hasExtra("uri")) { |
| 428 |
boolean isVideo = data.getBooleanExtra("video", false); |
| 429 |
if (isVideo) {
|
| 430 |
onVideoSelected(data.getStringExtra("uri"), null); |
| 431 |
} else {
|
| 432 |
onPhotoSelected(data.getStringExtra("uri"), null); |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
private void deleteTmpFile () |
| 439 |
{
|
| 440 |
try {
|
| 441 |
File storageDir = new File(getCacheDir(), CAMERA_CAPTURE_DIRNAME); |
| 442 |
if (!storageDir.exists()) {
|
| 443 |
storageDir.mkdir(); |
| 444 |
} |
| 445 |
File image = new File(storageDir, CAMERA_CAPTURE_FILENAME); |
| 446 |
if (image.exists()) {
|
| 447 |
image.delete(); |
| 448 |
} |
| 449 |
} catch (Exception ignored) {} |
| 450 |
} |
| 451 |
} |