Java tutorial
/** * Copyright 2016 Google Inc. All Rights Reserved. * * 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 com.demo.firebase; import android.Manifest; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v4.content.FileProvider; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Locale; import java.util.UUID; import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.EasyPermissions; /** * Activity to upload and download photos from Firebase Storage. * * See {@link MyUploadService} for upload example. * See {@link MyDownloadService} for download example. */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "Storage#MainActivity"; private static final int RC_TAKE_PICTURE = 101; private static final int RC_STORAGE_PERMS = 102; private static final String KEY_FILE_URI = "key_file_uri"; private static final String KEY_DOWNLOAD_URL = "key_download_url"; private BroadcastReceiver mBroadcastReceiver; private ProgressDialog mProgressDialog; private FirebaseAuth mAuth; private Uri mDownloadUrl = null; private Uri mFileUri = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance(); // Click listeners findViewById(R.id.button_camera).setOnClickListener(this); findViewById(R.id.button_sign_in).setOnClickListener(this); findViewById(R.id.button_download).setOnClickListener(this); // Restore instance state if (savedInstanceState != null) { mFileUri = savedInstanceState.getParcelable(KEY_FILE_URI); mDownloadUrl = savedInstanceState.getParcelable(KEY_DOWNLOAD_URL); } onNewIntent(getIntent()); // Local broadcast receiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive:" + intent); hideProgressDialog(); switch (intent.getAction()) { case MyDownloadService.DOWNLOAD_COMPLETED: // Get number of bytes downloaded long numBytes = intent.getLongExtra(MyDownloadService.EXTRA_BYTES_DOWNLOADED, 0); // Alert success showMessageDialog(getString(R.string.success), String.format(Locale.getDefault(), "%d bytes downloaded from %s", numBytes, intent.getStringExtra(MyDownloadService.EXTRA_DOWNLOAD_PATH))); break; case MyDownloadService.DOWNLOAD_ERROR: // Alert failure showMessageDialog("Error", String.format(Locale.getDefault(), "Failed to download from %s", intent.getStringExtra(MyDownloadService.EXTRA_DOWNLOAD_PATH))); break; case MyUploadService.UPLOAD_COMPLETED: case MyUploadService.UPLOAD_ERROR: onUploadResultIntent(intent); break; } } }; } @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); // Check if this Activity was launched by clicking on an upload notification if (intent.hasExtra(MyUploadService.EXTRA_DOWNLOAD_URL)) { onUploadResultIntent(intent); } } @Override public void onStart() { super.onStart(); updateUI(mAuth.getCurrentUser()); // Register receiver for uploads and downloads LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this); manager.registerReceiver(mBroadcastReceiver, MyDownloadService.getIntentFilter()); manager.registerReceiver(mBroadcastReceiver, MyUploadService.getIntentFilter()); } @Override public void onStop() { super.onStop(); // Unregister download receiver LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver); } @Override public void onSaveInstanceState(Bundle out) { out.putParcelable(KEY_FILE_URI, mFileUri); out.putParcelable(KEY_DOWNLOAD_URL, mDownloadUrl); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data); if (requestCode == RC_TAKE_PICTURE) { if (resultCode == RESULT_OK) { if (mFileUri != null) { uploadFromUri(mFileUri); } else { Log.w(TAG, "File URI is null"); } } else { Toast.makeText(this, "Taking picture failed.", Toast.LENGTH_SHORT).show(); } } } private void uploadFromUri(Uri fileUri) { Log.d(TAG, "uploadFromUri:src:" + fileUri.toString()); // Save the File URI mFileUri = fileUri; // Clear the last download, if any updateUI(mAuth.getCurrentUser()); mDownloadUrl = null; // Toast message in case the user does not see the notificatio Toast.makeText(this, "Uploading...", Toast.LENGTH_SHORT).show(); // Start MyUploadService to upload the file, so that the file is uploaded // even if this Activity is killed or put in the background startService(new Intent(this, MyUploadService.class).putExtra(MyUploadService.EXTRA_FILE_URI, fileUri) .setAction(MyUploadService.ACTION_UPLOAD)); } private void beginDownload() { // Get path String path = "photos/" + mFileUri.getLastPathSegment(); // Kick off MyDownloadService to download the file Intent intent = new Intent(this, MyDownloadService.class) .putExtra(MyDownloadService.EXTRA_DOWNLOAD_PATH, path).setAction(MyDownloadService.ACTION_DOWNLOAD); startService(intent); // Show loading spinner showProgressDialog(); } @AfterPermissionGranted(RC_STORAGE_PERMS) private void launchCamera() { Log.d(TAG, "launchCamera"); // Check that we have permission to read images from external storage. String perm = Manifest.permission.WRITE_EXTERNAL_STORAGE; if (!EasyPermissions.hasPermissions(this, perm)) { EasyPermissions.requestPermissions(this, getString(R.string.rationale_storage), RC_STORAGE_PERMS, perm); return; } // Choose file storage location, must be listed in res/xml/file_paths.xml File dir = new File(Environment.getExternalStorageDirectory() + "/photos"); File file = new File(dir, UUID.randomUUID().toString() + ".jpg"); try { // Create directory if it does not exist. if (!dir.exists()) { dir.mkdir(); } boolean created = file.createNewFile(); Log.d(TAG, "file.createNewFile:" + file.getAbsolutePath() + ":" + created); } catch (IOException e) { Log.e(TAG, "file.createNewFile" + file.getAbsolutePath() + ":FAILED", e); } // Create content:// URI for file, required since Android N // See: https://developer.android.com/reference/android/support/v4/content/FileProvider.html mFileUri = FileProvider.getUriForFile(this, "com.google.firebase.quickstart.firebasestorage.fileprovider", file); // Create and launch the intent Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri); // Grant permission to camera (this is required on KitKat and below) List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resolveInfos) { String packageName = resolveInfo.activityInfo.packageName; grantUriPermission(packageName, mFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); } // Start picture-taking intent startActivityForResult(takePictureIntent, RC_TAKE_PICTURE); } private void signInAnonymously() { // Sign in anonymously. Authentication is required to read or write from Firebase Storage. showProgressDialog(); mAuth.signInAnonymously().addOnSuccessListener(this, new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { Log.d(TAG, "signInAnonymously:SUCCESS"); hideProgressDialog(); updateUI(authResult.getUser()); } }).addOnFailureListener(this, new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { Log.e(TAG, "signInAnonymously:FAILURE", exception); hideProgressDialog(); updateUI(null); } }); } private void onUploadResultIntent(Intent intent) { // Got a new intent from MyUploadService with a success or failure mDownloadUrl = intent.getParcelableExtra(MyUploadService.EXTRA_DOWNLOAD_URL); mFileUri = intent.getParcelableExtra(MyUploadService.EXTRA_FILE_URI); updateUI(mAuth.getCurrentUser()); } private void updateUI(FirebaseUser user) { // Signed in or Signed out if (user != null) { findViewById(R.id.layout_signin).setVisibility(View.GONE); findViewById(R.id.layout_storage).setVisibility(View.VISIBLE); } else { findViewById(R.id.layout_signin).setVisibility(View.VISIBLE); findViewById(R.id.layout_storage).setVisibility(View.GONE); } // Download URL and Download button if (mDownloadUrl != null) { ((TextView) findViewById(R.id.picture_download_uri)).setText(mDownloadUrl.toString()); findViewById(R.id.layout_download).setVisibility(View.VISIBLE); } else { ((TextView) findViewById(R.id.picture_download_uri)).setText(null); findViewById(R.id.layout_download).setVisibility(View.GONE); } } private void showMessageDialog(String title, String message) { AlertDialog ad = new AlertDialog.Builder(this).setTitle(title).setMessage(message).create(); ad.show(); } private void showProgressDialog() { if (mProgressDialog == null) { mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("Loading..."); mProgressDialog.setIndeterminate(true); } mProgressDialog.show(); } private void hideProgressDialog() { if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int i = item.getItemId(); if (i == R.id.action_logout) { FirebaseAuth.getInstance().signOut(); updateUI(null); return true; } else { return super.onOptionsItemSelected(item); } } @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.button_camera) { launchCamera(); } else if (i == R.id.button_sign_in) { signInAnonymously(); } else if (i == R.id.button_download) { beginDownload(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } }