Java tutorial
package com.jigarmjoshi; import java.io.File; import java.io.IOException; import java.util.TimerTask; import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.location.Location; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapView; import com.google.android.gms.maps.MapsInitializer; import com.jigarmjoshi.database.EntryDao; import com.jigarmjoshi.model.Report; import com.jigarmjoshi.service.EntryService; import com.jigarmjoshi.utils.FlagState; import com.jigarmjoshi.utils.Utility; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to you 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. * * @author jigar.joshi * */ public class ReportFragment extends Fragment implements OnClickListener { Button p1ReportButton; Button p2ReportButton; Button p3ReportButton; ImageView reportImageView; View rootView = null; Bitmap cameraIconBitMap = null; private static final int TAKE_PHOTO_CODE = 1; private static String lastImagePath = null; MapView mapView; static GoogleMap map; TimerTask uploaderTask; private static final String TAG = ReportFragment.class.getSimpleName(); public static GoogleMap getReportGoogleMap() { return map; } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_report, container, false); reportImageView = (ImageView) rootView.findViewById(R.id.reportImageView); this.cameraIconBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.cam); p1ReportButton = (Button) rootView.findViewById(R.id.buttonReportP1); p1ReportButton.setBackgroundColor(Color.RED); p1ReportButton.setTextColor(Color.BLACK); p2ReportButton = (Button) rootView.findViewById(R.id.buttonReportP2); p2ReportButton.setBackgroundColor(Color.rgb(255, 100, 0)); // orange p2ReportButton.setTextColor(Color.BLACK); p3ReportButton = (Button) rootView.findViewById(R.id.buttonReportP3); p3ReportButton.setBackgroundColor(Color.rgb(255, 150, 0)); p3ReportButton.setTextColor(Color.BLACK); reportImageView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { File newFile = getImageFile(); try { newFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } Uri outputFileUri = Uri.fromFile(newFile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); p1ReportButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performAction(Utility.P1); } }); p2ReportButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performAction(Utility.P2); } }); p3ReportButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performAction(Utility.P3); } }); // Gets the MapView from the XML layout and creates it mapView = (MapView) rootView.findViewById(R.id.mapviewReport); mapView.onCreate(savedInstanceState); // Gets to GoogleMap from the MapView and does initialization stuff map = mapView.getMap(); map.getUiSettings().setMyLocationButtonEnabled(true); // map.setMyLocationEnabled(true); mapView.refreshDrawableState(); // Needs to call MapsInitializer before doing any CameraUpdateFactory // calls try { MapsInitializer.initialize(this.getActivity()); } catch (Exception e) { e.printStackTrace(); } Utility.focusAtCurrentLocation(map); return rootView; } @Override public void onResume() { mapView.onResume(); super.onResume(); } @Override public void onDestroy() { mapView.onDestroy(); super.onDestroy(); } @Override public void onPause() { super.onPause(); mapView.onPause(); } @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); } private File getImageFile() { String path = Environment.getExternalStorageDirectory().toString(); File dir = new File(path, "/cleanup-India/media/cleanup-India/"); if (!dir.isDirectory()) { dir.mkdirs(); } File file = new File(dir, String.valueOf(System.currentTimeMillis()) + ".jpg"); String imagePath = file.getAbsolutePath(); lastImagePath = imagePath; // scan the image so show up in album MediaScannerConnection.scanFile(rootView.getContext(), new String[] { imagePath }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { } }); return file; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PHOTO_CODE && resultCode == Activity.RESULT_OK) { // Bitmap photo = (Bitmap) data.getExtras().get("data"); // reportImageView.setImageBitmap(photo); reportImageView.setImageBitmap(BitmapFactory.decodeFile(lastImagePath)); } else if (resultCode == Activity.RESULT_CANCELED) { Toast.makeText(rootView.getContext(), "Picture was not taken ", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(rootView.getContext(), "Picture was not taken ", Toast.LENGTH_SHORT).show(); } } @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } private void disableActionButtons() { p1ReportButton.setEnabled(false); p2ReportButton.setEnabled(false); p3ReportButton.setEnabled(false); } private void enableActionButtons() { p1ReportButton.setEnabled(true); p2ReportButton.setEnabled(true); p3ReportButton.setEnabled(true); } private void performAction(final String priority) { Location location = FlagState.getLastDetectedLocation(); Utility.checkAndEnableLocation(rootView.getContext()); if (location == null) { Toast.makeText(rootView.getContext(), rootView.getContext().getString(R.string.missing_location), Toast.LENGTH_SHORT).show(); return; } if (lastImagePath == null) { Toast.makeText(rootView.getContext(), rootView.getContext().getString(R.string.missing_image), Toast.LENGTH_SHORT).show(); return; } disableActionButtons(); Log.i(TAG, "constructing a entry : " + priority); Report entry = EntryService.getInstance().constructEntry(location.getLatitude(), location.getLongitude(), lastImagePath, rootView.getContext(), priority); EntryDao.getInstance(rootView.getContext()).saveEntry(entry); Toast.makeText(rootView.getContext(), getResources().getString(R.string.entry_saved), Toast.LENGTH_SHORT) .show(); reportImageView.setImageBitmap(cameraIconBitMap); lastImagePath = null; enableActionButtons(); } }