Java tutorial
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.awesomeapp.messenger.ui; import android.Manifest; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.drawable.ColorDrawable; import android.media.MediaRecorder; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.RemoteException; import android.preference.PreferenceManager; import android.provider.MediaStore; import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import org.awesomeapp.messenger.ImApp; import org.awesomeapp.messenger.MainActivity; import org.awesomeapp.messenger.provider.Imps; import org.awesomeapp.messenger.service.IChatSession; import org.awesomeapp.messenger.tasks.AddContactAsyncTask; import org.awesomeapp.messenger.ui.onboarding.OnboardingActivity; import org.awesomeapp.messenger.ui.onboarding.OnboardingManager; import org.awesomeapp.messenger.util.SecureMediaStore; import org.awesomeapp.messenger.util.SystemServices; import org.awesomeapp.messenger.util.XmppUriHelper; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import im.zom.messenger.R; public class GalleryActivity extends BaseActivity { private ImApp mApp; private Uri mLastPhoto; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.awesome_activity_gallery); setTitle(R.string.photo_gallery); Intent intent = getIntent(); mApp = (ImApp) getApplication(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startPhotoTaker(); } }); applyStyleForToolbar(); } public void applyStyleForToolbar() { //not set color final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); int selColor = settings.getInt("themeColor", -1); if (selColor != -1) { if (Build.VERSION.SDK_INT >= 21) { getWindow().setNavigationBarColor(selColor); getWindow().setStatusBarColor(selColor); } getSupportActionBar().setBackgroundDrawable(new ColorDrawable(selColor)); } } private final static int MY_PERMISSIONS_REQUEST_CAMERA = 707; void startPhotoTaker() { int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA); if (permissionCheck == PackageManager.PERMISSION_DENIED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { View view = findViewById(R.id.gallery_fragment); // Show an expanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. Snackbar.make(view, R.string.grant_perms, Snackbar.LENGTH_LONG).show(); } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA }, MY_PERMISSIONS_REQUEST_CAMERA); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } } else { // create Intent to take a picture and return control to the calling application Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "cs_" + new Date().getTime() + ".jpg"); mLastPhoto = Uri.fromFile(photo); intent.putExtra(MediaStore.EXTRA_OUTPUT, mLastPhoto); // start the image capture Intent startActivityForResult(intent, ConversationDetailActivity.REQUEST_TAKE_PICTURE); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_CAMERA: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == ConversationDetailActivity.REQUEST_TAKE_PICTURE) { if (mLastPhoto != null) importPhoto(); } } } private void importPhoto() { // import SystemServices.FileInfo info = SystemServices.getFileInfoFromURI(this, mLastPhoto); String sessionId = "self"; String offerId = UUID.randomUUID().toString(); try { Uri vfsUri = SecureMediaStore.resizeAndImportImage(this, sessionId, mLastPhoto, info.type); delete(mLastPhoto); //adds in an empty message, so it can exist in the gallery and be forwarded Imps.insertMessageInDb(getContentResolver(), false, new Date().getTime(), true, null, vfsUri.toString(), System.currentTimeMillis(), Imps.MessageType.OUTGOING_ENCRYPTED_VERIFIED, 0, offerId, info.type); mLastPhoto = null; } catch (IOException ioe) { Log.e(ImApp.LOG_TAG, "error importing photo", ioe); } } private boolean delete(Uri uri) { if (uri.getScheme().equals("content")) { int deleted = getContentResolver().delete(uri, null, null); return deleted == 1; } if (uri.getScheme().equals("file")) { java.io.File file = new java.io.File(uri.toString().substring(5)); return file.delete(); } return false; } @Override protected void onResume() { super.onResume(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { // getMenuInflater().inflate(R.menu.menu_conversation_detail, menu); return true; } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); } }