Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tubankab.diskominfo.taprose.menu.home.view;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.PorterDuff;
- import android.graphics.drawable.Drawable;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.ImageView;
- import android.widget.TextView;
- import androidx.annotation.Nullable;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
- import androidx.core.graphics.drawable.RoundedBitmapDrawable;
- import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
- import com.anupcowkur.reservoir.Reservoir;
- import com.anupcowkur.reservoir.ReservoirGetCallback;
- import com.anupcowkur.reservoir.ReservoirPutCallback;
- import com.bumptech.glide.Glide;
- import com.bumptech.glide.request.animation.GlideAnimation;
- import com.bumptech.glide.request.target.BitmapImageViewTarget;
- import com.bumptech.glide.request.target.SimpleTarget;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.BitmapDescriptorFactory;
- import com.google.android.gms.maps.model.CameraPosition;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.Marker;
- import com.google.android.gms.maps.model.MarkerOptions;
- import com.google.android.material.bottomsheet.BottomSheetDialog;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import tubankab.diskominfo.taprose.R;
- import tubankab.diskominfo.taprose.data.Constant;
- import tubankab.diskominfo.taprose.data.Profile;
- import tubankab.diskominfo.taprose.data.Settings;
- import tubankab.diskominfo.taprose.helper.ActivityHelper;
- import tubankab.diskominfo.taprose.helper.ActivityUtilHelper;
- import tubankab.diskominfo.taprose.helper.ForegroundRelativeLayout;
- import tubankab.diskominfo.taprose.menu.home.model.ListMapAduanModel;
- import tubankab.diskominfo.taprose.network.repositories.HomeRepository;
- /**
- * Created by yusuf on 3/8/17.
- */
- public class FragmentMapsLaporan extends SupportMapFragment implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
- private static final String RESERVOIR_MAPS_KEY = "reservoir_maps_key";
- private static final int REQUEST_LOCATION_CODE = 21;
- private Activity activity;
- private Marker marker;
- private List<Marker> listMarker = new ArrayList<>();
- private View mCustomMarkerView;
- private ImageView mMarkerImageView;
- private LayoutInflater inflater;
- private HomeRepository api;
- private boolean isAttached;
- BottomSheetDialog dialogDescription;
- ForegroundRelativeLayout dialogDescriptionLayout;
- ImageView dialogDescriptionImgView;
- TextView dialogDescriptionStatus;
- TextView dialogDescriptionName;
- TextView dialogDescriptionTitle;
- TextView dialogDescriptionDescript;
- TextView dialogDescriptionLike;
- TextView dialogDescriptionComment;
- TextView dialogDescriptionFeedback;
- TextView dialogDescriptionTime;
- @Override
- public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
- return super.onCreateView(layoutInflater, viewGroup, bundle);
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- this.activity = getActivity();
- this.api = new HomeRepository(activity);
- inflater = ((LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
- mCustomMarkerView = inflater.inflate(R.layout.item_maps_aduan, null);
- mMarkerImageView = mCustomMarkerView.findViewById(R.id.ivHomeItemMapsAduan);
- setupDescription();
- setHasOptionsMenu(true);
- getMapAsync(this);
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- googleMap.getUiSettings().setCompassEnabled(true);
- googleMap.getUiSettings().setZoomControlsEnabled(true);
- if (requestLocation()) {
- //show user location
- if (ActivityUtilHelper.isLocationEnabled(getContext())) {
- googleMap.setMyLocationEnabled(true);
- LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
- Criteria criteria = new Criteria();
- String bestProvider = locationManager.getBestProvider(criteria, true);
- if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
- && ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // Activity#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 Activity#requestPermissions for more details.
- return;
- }
- Location location = locationManager.getLastKnownLocation(bestProvider);
- if(location!=null){
- CameraPosition cameraPosition = new CameraPosition.Builder()
- .target(new LatLng(location.getLatitude(), location.getLongitude()))
- .zoom(15f)
- .build();
- googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
- }else{
- googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-7.041284, 112.064566)));
- googleMap.animateCamera(CameraUpdateFactory.zoomTo(9f));
- }
- }else{
- googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-7.041284, 112.064566)));
- googleMap.animateCamera(CameraUpdateFactory.zoomTo(9f));
- ActivityHelper.displayToast(getActivity(), getActivity().getResources().getString(R.string.all_msg_request_akses_lokasi), Constant.TOAST_WARNING);
- }
- }
- /*
- googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-6.894959, 112.065155)));
- googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));*/
- googleMap.setOnMarkerClickListener(this);
- //start request data onMap Ready
- requestData(googleMap);
- loadLocalDataV2(googleMap);
- }
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- super.onCreateOptionsMenu(menu, inflater);
- inflater.inflate(R.menu.menu_lap_maps, menu);
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- isAttached = true;
- }
- @Override
- public void onDetach() {
- super.onDetach();
- isAttached = false;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if(item.getItemId()==R.id.menuHomeMapsCategory){
- Intent i = new Intent(getContext(), MainActivityMapsCategory.class);
- startActivityForResult(i, Constant.MAP_CATEGORY_REQUEST);
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- Log.d(Constant.TAG_DEBUG,"fragment maps laporan on activity result");
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == Constant.MAP_CATEGORY_REQUEST) {
- if (data != null) {
- for (Marker mk : listMarker) {
- mk.setVisible(true);
- }
- Set<String> setListAduanId = new HashSet<>();
- if(data.getStringArrayListExtra("val").size()>0) {
- int[] arrayAduanID = new int[data.getStringArrayListExtra("val").size()];
- for (int i = 0; i < data.getStringArrayListExtra("val").size(); i++) {
- arrayAduanID[i] = Integer.parseInt(data.getStringArrayListExtra("val").get(i));
- setListAduanId.add(data.getStringArrayListExtra("val").get(i));
- }
- Settings.saveListCategoryAduanId(setListAduanId);
- Log.d(Constant.TAG_DEBUG,"id category laporan : "+ Arrays.toString(arrayAduanID));
- for(Marker mk:listMarker){
- mk.setVisible(false);
- TagObject tag = (TagObject) mk.getTag();
- for(int i:arrayAduanID){
- if(i==tag.categoryAduanId){
- mk.setVisible(true);
- }
- }
- }
- }
- }
- }
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- Settings.deleteListCategoryAduanId();
- Settings.saveIsFromMapsLaporanCategory(false);
- }
- /**
- * request data from server
- * @param googleMap google map
- */
- private void requestData(final GoogleMap googleMap){
- Calendar cal = Calendar.getInstance();
- int month = cal.get(Calendar.MONTH)+1 ;
- api.getListMapAduan(Profile.getToken(), Profile.getSocialId(), month, (context, object, status, statusCode) -> {
- if (status) {
- ListMapAduanModel model = (ListMapAduanModel) object;
- if (model.lastUpdate != null) {
- if (!model.lastUpdate.equals(Settings.getLastUpdateMapAduan())) {
- //update localdata
- Settings.saveLastUpdateMapAduan(model.lastUpdate);
- Reservoir.putAsync(RESERVOIR_MAPS_KEY, model, new ReservoirPutCallback() {
- @Override
- public void onSuccess() {
- loadLocalDataV2(googleMap);
- }
- @Override
- public void onFailure(Exception e) {
- Log.e(Constant.TAG_ERROR, "failed write object " + e.getMessage());
- }
- });
- }
- }
- } else {
- ActivityHelper.displayToast(context, context.getResources().getString(R.string.all_msg_load_map_aduan_failed), Constant.TOAST_ERROR);
- }
- });
- }
- /**
- * load local data
- * @param googleMap googleMap
- */
- public void loadLocalDataV2(final GoogleMap googleMap){
- Reservoir.getAsync(RESERVOIR_MAPS_KEY, ListMapAduanModel.class, new ReservoirGetCallback<ListMapAduanModel>() {
- @Override
- public void onSuccess(ListMapAduanModel listMapAduanModel) {
- if(listMapAduanModel.data.size()>0) {
- for (int i = 0; i < listMapAduanModel.data.size(); i++) {
- // JIKA TIDAK MEMILIKI TITIK LOKASI (LAT,LONG) , MAKA TIDAK DIMUNCULKAN DI PETA
- if(listMapAduanModel.data.get(i).lat != null && listMapAduanModel.data.get(i).lon != null) {
- int categoryID = 0;
- if(listMapAduanModel.data.get(i).aduanCategoryId != null){
- categoryID = listMapAduanModel.data.get(i).aduanCategoryId;
- }else{
- categoryID = listMapAduanModel.data.get(i).category.id;
- }
- addCustomMarker(googleMap, listMapAduanModel.data.get(i).id,
- categoryID,
- listMapAduanModel.data.get(i).category.iconMarker,
- listMapAduanModel.data.get(i).lat,
- listMapAduanModel.data.get(i).lon);
- }
- }
- }
- }
- @Override
- public void onFailure(Exception e) {
- Log.e(Constant.TAG_ERROR, "loadLocalDataV2 # get object failed "+e.getMessage());
- }
- });
- }
- private void addCustomMarker(final GoogleMap googleMap, final int id, final int categoryId, final String picture, final float longitude, final float latitiude){
- if(googleMap!=null) {
- if(isAttached) {
- Glide.with(activity).
- load(picture)
- .asBitmap()
- .fitCenter()
- .into(new SimpleTarget<Bitmap>() {
- @Override
- public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
- MarkerOptions markerOpt = new MarkerOptions()
- .position(new LatLng(longitude, latitiude))
- .icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(mCustomMarkerView, bitmap)));
- marker = googleMap.addMarker(markerOpt);
- TagObject tagObject = new TagObject(id, categoryId);
- marker.setTag(tagObject);
- listMarker.add(marker);
- }
- });
- }
- }
- }
- private Bitmap getMarkerBitmapFromView(View view, Bitmap bitmap) {
- mMarkerImageView.setImageBitmap(bitmap);
- view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
- view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
- view.buildDrawingCache();
- Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
- Bitmap.Config.ARGB_8888);
- Canvas canvas = new Canvas(returnedBitmap);
- canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
- Drawable drawable = view.getBackground();
- if (drawable != null)
- drawable.draw(canvas);
- view.draw(canvas);
- return returnedBitmap;
- }
- @Override
- public boolean onMarkerClick(Marker marker) {
- final TagObject tag = (TagObject) marker.getTag();
- Reservoir.getAsync(RESERVOIR_MAPS_KEY, ListMapAduanModel.class, new ReservoirGetCallback<ListMapAduanModel>() {
- @Override
- public void onSuccess(ListMapAduanModel listMapAduanModel) {
- for(int i=0;i<listMapAduanModel.data.size();i++){
- if(listMapAduanModel.data.get(i).id==tag.aduanId){
- showDescription(listMapAduanModel.data.get(i));
- break;
- }
- }
- }
- @Override
- public void onFailure(Exception e) {
- Log.e(Constant.TAG_ERROR,"marker click error "+e.getMessage());
- }
- });
- return false;
- }
- private void setupDescription(){
- dialogDescription = new BottomSheetDialog(activity);
- View v = inflater.inflate(R.layout.view_aduan_location, null);
- dialogDescription.setContentView(v);
- dialogDescriptionLayout = v.findViewById(R.id.layoutAduan);
- dialogDescriptionImgView = v.findViewById(R.id.ivHomeViewAduanProfile);
- dialogDescriptionStatus = v.findViewById(R.id.tvHomeViewAduanStatus);
- dialogDescriptionName = v.findViewById(R.id.tvHomeViewAduanName);
- dialogDescriptionTitle = v.findViewById(R.id.tvHomeViewAduanTitle);
- dialogDescriptionDescript = v.findViewById(R.id.tvHomeViewAduanDescription);
- dialogDescriptionLike = v.findViewById(R.id.tvHomeViewAduanLike);
- dialogDescriptionComment = v.findViewById(R.id.tvHomeViewAduanComment);
- dialogDescriptionFeedback = v.findViewById(R.id.tvHomeViewAduanFeedback);
- dialogDescriptionTime = v.findViewById(R.id.tvHomeViewAduanTime);
- }
- private void showDescription(final ListMapAduanModel.Datum dataAduan){
- if (!dialogDescription.isShowing()){
- dialogDescriptionLayout.setOnClickListener(view -> {
- Intent i = new Intent(activity, MainMasyarakatDetail.class);
- i.putExtra(MainMasyarakatDetail.FROM_MAPS, true);
- i.putExtra(MainMasyarakatDetail.ID_ADUAN, dataAduan.id);
- //other data
- i.putExtra(MainMasyarakatDetail.USER_ID, dataAduan.createdBy.id);
- i.putExtra(MainMasyarakatDetail.TITLE, dataAduan.title);
- i.putExtra(MainMasyarakatDetail.PICTURE, dataAduan.picture);
- i.putExtra(MainMasyarakatDetail.LOCATION, dataAduan.location);
- i.putExtra(MainMasyarakatDetail.DESCRIPTION, dataAduan.description);
- i.putExtra(MainMasyarakatDetail.LIKE, dataAduan.totalLikes);
- i.putExtra(MainMasyarakatDetail.COMMENT, dataAduan.totalComments);
- i.putExtra(MainMasyarakatDetail.FEEDBACK, dataAduan.totalFeedback);
- i.putExtra(MainMasyarakatDetail.STATUS, dataAduan.status);
- i.putExtra(MainMasyarakatDetail.NAME_PERSON, dataAduan.createdBy.fullname);
- i.putExtra(MainMasyarakatDetail.IMG_PERSON, dataAduan.createdBy.picture);
- i.putExtra(MainMasyarakatDetail.CATEGORY, dataAduan.category.name);
- i.putExtra(MainMasyarakatDetail.IC_CATEGORY, dataAduan.category.icon);
- i.putExtra(MainMasyarakatDetail.CREATED_AT, dataAduan.createdAt);
- i.putExtra(MainMasyarakatDetail.NAME_CATEGORY, dataAduan.category.name);
- i.putExtra(MainMasyarakatDetail.IS_HIDDEN, dataAduan.isHidden);
- activity.startActivity(i);
- });
- Glide.with(dialogDescriptionImgView.getContext())
- .load(dataAduan.createdBy.picture).asBitmap().centerCrop()
- .placeholder(R.drawable.no_image_profile)
- .error(R.drawable.no_image_profile)
- .into(new BitmapImageViewTarget(dialogDescriptionImgView) {
- @Override
- protected void setResource(Bitmap resource) {
- //super.setResource(resource);
- RoundedBitmapDrawable circleImg =
- RoundedBitmapDrawableFactory.create(dialogDescriptionImgView.getContext().getResources(), resource);
- circleImg.setCircular(true);
- dialogDescriptionImgView.setImageDrawable(circleImg);
- }
- });
- setStatus(dialogDescriptionStatus, String.valueOf(dataAduan.status));
- dialogDescriptionName.setText(dataAduan.createdBy.fullname);
- dialogDescriptionTitle.setText(dataAduan.title);
- dialogDescriptionDescript.setText(dataAduan.description);
- dialogDescriptionLike.setText(String.valueOf(dataAduan.totalLikes));
- dialogDescriptionComment.setText(String.valueOf(dataAduan.totalComments));
- dialogDescriptionFeedback.setText(String.valueOf(dataAduan.totalFeedback));
- String time = "";
- time = ActivityHelper.getTimeHuman(dataAduan.createdAt);
- dialogDescriptionTime.setText(time);
- dialogDescription.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
- dialogDescription.show();
- }
- }
- private class TagObject{
- public int aduanId;
- public int categoryAduanId;
- TagObject(int aduanId, int categoryAduanId){
- this.aduanId = aduanId;
- this.categoryAduanId = categoryAduanId;
- }
- }
- private void setStatus(TextView textView, String status){
- if(status.toLowerCase().equals("menunggu")){
- textView.setBackground(ContextCompat.getDrawable(activity, R.drawable.bg_laporan_status_red));
- }else if(status.toLowerCase().equals("proses")){
- textView.setBackground(ContextCompat.getDrawable(activity, R.drawable.bg_laporan_status_yellow));
- }else{
- textView.setBackground(ContextCompat.getDrawable(activity, R.drawable.bg_laporan_status_green));
- }
- textView.setText(status);
- }
- private boolean requestLocation(){
- if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
- if(getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
- if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)){
- ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_CODE);
- return false;
- }else{
- ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_CODE);
- return false;
- }
- }else{
- return true;
- }
- }else{
- return true;
- }
- }
- }
RAW Paste Data