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.barak.pix; import android.Manifest; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Bitmap; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.UUID; import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.EasyPermissions; public class NewPostActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks, NewPostUploadTaskFragment.TaskCallbacks { public static final String TAG = "NewPostActivity"; public static final String TAG_TASK_FRAGMENT = "newPostUploadTaskFragment"; private static final int THUMBNAIL_MAX_DIMENSION = 640; private static final int FULL_SIZE_MAX_DIMENSION = 1280; private Button mSubmitButton; private ImageView mImageView; private Uri mFileUri; private Bitmap mResizedBitmap; private Bitmap mThumbnail; private NewPostUploadTaskFragment mTaskFragment; private static final int TC_PICK_IMAGE = 101; private static final int RC_CAMERA_PERMISSIONS = 102; private static final String[] cameraPerms = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }; private LocationManager locationManager; private MyLocationListener locationListener; private Location mLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_post); // find the retained fragment on activity restarts FragmentManager fm = getSupportFragmentManager(); mTaskFragment = (NewPostUploadTaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT); // create the fragment and data the first time if (mTaskFragment == null) { // add the fragment mTaskFragment = new NewPostUploadTaskFragment(); fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit(); } mImageView = (ImageView) findViewById(R.id.new_post_picture); mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showImagePicker(); } }); Bitmap selectedBitmap = mTaskFragment.getSelectedBitmap(); Bitmap thumbnail = mTaskFragment.getThumbnail(); if (selectedBitmap != null) { mImageView.setImageBitmap(selectedBitmap); mResizedBitmap = selectedBitmap; } if (thumbnail != null) { mThumbnail = thumbnail; } final EditText descriptionText = (EditText) findViewById(R.id.new_post_text); mSubmitButton = (Button) findViewById(R.id.new_post_submit); mSubmitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { if (mResizedBitmap == null) { Toast.makeText(NewPostActivity.this, " !!!", Toast.LENGTH_SHORT).show(); return; } if (mLocation == null) { Toast.makeText(NewPostActivity.this, " ? ?", Toast.LENGTH_SHORT) .show(); return; } String postText = descriptionText.getText().toString(); if (TextUtils.isEmpty(postText)) { descriptionText.setError(getString(R.string.error_required_field)); return; } showProgressDialog(getString(R.string.post_upload_progress_message)); mSubmitButton.setEnabled(false); Long timestamp = System.currentTimeMillis(); String bitmapPath = "https://pictures-e88fc.firebaseio.com" + "/" + FirebaseUtil.getCurrentUserId() + "/full/" + timestamp.toString() + "/"; String thumbnailPath = "https://pictures-e88fc.firebaseio.com" + "/" + FirebaseUtil.getCurrentUserId() + "/thumb/" + timestamp.toString() + "/"; mTaskFragment.uploadPost(mResizedBitmap, bitmapPath, mThumbnail, thumbnailPath, mFileUri.getLastPathSegment(), postText, mLocation.getLatitude(), mLocation.getLongitude()); } }); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = new MyLocationListener(); if (checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION, getApplicationContext(), NewPostActivity.this)) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener); } else { requestPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION, 11, getApplicationContext(), this); } } @Override public void onPostUploaded(final String error) { NewPostActivity.this.runOnUiThread(new Runnable() { @Override public void run() { mSubmitButton.setEnabled(true); dismissProgressDialog(); if (error == null) { Toast.makeText(NewPostActivity.this, "Post created!", Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(NewPostActivity.this, error, Toast.LENGTH_SHORT).show(); } } }); } @AfterPermissionGranted(RC_CAMERA_PERMISSIONS) private void showImagePicker() { // Check for camera permissions if (!EasyPermissions.hasPermissions(this, cameraPerms)) { EasyPermissions.requestPermissions(this, "This sample will upload a picture from your Camera", RC_CAMERA_PERMISSIONS, cameraPerms); return; } // Choose file storage location File file = new File(getExternalCacheDir(), UUID.randomUUID().toString()); mFileUri = Uri.fromFile(file); // Camera final List<Intent> cameraIntents = new ArrayList<Intent>(); final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = getPackageManager(); final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); for (ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent intent = new Intent(captureIntent); intent.setComponent(new ComponentName(packageName, res.activityInfo.name)); intent.setPackage(packageName); intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri); cameraIntents.add(intent); } // Image Picker Intent pickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Intent chooserIntent = Intent.createChooser(pickerIntent, getString(R.string.picture_chooser_title)); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()])); startActivityForResult(chooserIntent, TC_PICK_IMAGE); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TC_PICK_IMAGE) { if (resultCode == RESULT_OK) { final boolean isCamera; if (data == null || data.getData() == null) { isCamera = true; } else { isCamera = MediaStore.ACTION_IMAGE_CAPTURE.equals(data.getAction()); } if (!isCamera && data != null) { mFileUri = data.getData(); } if (mFileUri != null) { mTaskFragment.resizeBitmap(mFileUri, THUMBNAIL_MAX_DIMENSION); mTaskFragment.resizeBitmap(mFileUri, FULL_SIZE_MAX_DIMENSION); } } } } @Override public void onDestroy() { // store the data in the fragment if (mResizedBitmap != null) { mTaskFragment.setSelectedBitmap(mResizedBitmap); } if (mThumbnail != null) { mTaskFragment.setThumbnail(mThumbnail); } super.onDestroy(); } @Override public void onBitmapResized(Bitmap resizedBitmap, int mMaxDimension) { if (resizedBitmap == null) { Log.e(TAG, "Couldn't resize bitmap in background task."); Toast.makeText(getApplicationContext(), "Couldn't resize bitmap.", Toast.LENGTH_SHORT).show(); return; } if (mMaxDimension == THUMBNAIL_MAX_DIMENSION) { mThumbnail = resizedBitmap; } else if (mMaxDimension == FULL_SIZE_MAX_DIMENSION) { mResizedBitmap = resizedBitmap; mImageView.setImageBitmap(mResizedBitmap); } if (mThumbnail != null && mResizedBitmap != null) { mSubmitButton.setEnabled(true); } } @Override public void onPermissionsGranted(int requestCode, List<String> perms) { } @Override public void onPermissionsDenied(int requestCode, List<String> perms) { } private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { mLocation = loc; Toast.makeText(NewPostActivity.this, "? ?", Toast.LENGTH_SHORT).show(); if (ActivityCompat.checkSelfPermission(NewPostActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.removeUpdates(this); if (mTaskFragment == null) { throw new NullPointerException(" mTaskFragment == null "); } String cityName = null; Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (addresses.size() > 0) { System.out.println(addresses.get(0).getLocality()); cityName = addresses.get(0).getLocality(); } Toast.makeText(NewPostActivity.this, cityName, Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onProviderDisabled(String provider) { Toast.makeText(NewPostActivity.this, "onProviderDisabled", Toast.LENGTH_LONG).show(); final AlertDialog.Builder builder = new AlertDialog.Builder(NewPostActivity.this); builder.setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }).setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { dialog.cancel(); } }); final AlertDialog alert = builder.create(); alert.show(); } @Override public void onProviderEnabled(String provider) { Toast.makeText(NewPostActivity.this, "onProviderEnabled", Toast.LENGTH_LONG).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } public void requestPermission(String strPermission, int perCode, Context _c, Activity _a) { if (ActivityCompat.shouldShowRequestPermissionRationale(_a, strPermission)) { Toast.makeText(NewPostActivity.this, "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); } else { ActivityCompat.requestPermissions(_a, new String[] { strPermission }, perCode); } } public static boolean checkPermission(String strPermission, Context _c, Activity _a) { int result = ContextCompat.checkSelfPermission(_c, strPermission); if (result == PackageManager.PERMISSION_GRANTED) { return true; } else { return false; } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 11: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, locationListener); } else { Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG).show(); } break; } } }