Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 30.87 KB | None | 0 0
  1. package com.unej.sisterforlecturer.fragment;
  2.  
  3.  
  4. import android.Manifest;
  5. import android.bluetooth.BluetoothAdapter;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.content.pm.PackageManager;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.provider.Settings;
  14. import android.support.annotation.NonNull;
  15. import android.support.annotation.Nullable;
  16. import android.support.design.widget.Snackbar;
  17. import android.support.v4.app.Fragment;
  18. import android.support.v7.app.AlertDialog;
  19. import android.text.TextUtils;
  20. import android.util.Log;
  21. import android.view.LayoutInflater;
  22. import android.view.View;
  23. import android.view.ViewGroup;
  24. import android.view.ViewTreeObserver;
  25. import android.widget.FrameLayout;
  26. import android.widget.ImageView;
  27. import android.widget.ProgressBar;
  28. import android.widget.TextView;
  29. import android.widget.Toast;
  30.  
  31. import com.mist.android.AppMode;
  32. import com.mist.android.MSTAsset;
  33. import com.mist.android.MSTBeacon;
  34. import com.mist.android.MSTCentralManagerIndoorOnlyListener;
  35. import com.mist.android.MSTCentralManagerStatusCode;
  36. import com.mist.android.MSTClient;
  37. import com.mist.android.MSTMap;
  38. import com.mist.android.MSTPoint;
  39. import com.mist.android.MSTVirtualBeacon;
  40. import com.mist.android.MSTZone;
  41. import com.squareup.picasso.Callback;
  42. import com.squareup.picasso.NetworkPolicy;
  43. import com.squareup.picasso.Picasso;
  44. import com.unej.sisterforlecturer.R;
  45. import com.unej.sisterforlecturer.activity.LoginActivity;
  46. import com.unej.sisterforlecturer.activity.PresensiQRActivity;
  47. import com.unej.sisterforlecturer.app.MainApplication;
  48. import com.unej.sisterforlecturer.model.BleModel;
  49. import com.unej.sisterforlecturer.service.ApiService;
  50. import com.unej.sisterforlecturer.utils.MistManager;
  51. import com.unej.sisterforlecturer.utils.Server;
  52. import com.unej.sisterforlecturer.utils.Utils;
  53.  
  54. import org.json.JSONArray;
  55. import org.json.JSONException;
  56. import org.json.JSONObject;
  57.  
  58. import java.util.ArrayList;
  59. import java.util.Date;
  60. import java.util.HashMap;
  61. import java.util.List;
  62.  
  63. import butterknife.BindColor;
  64. import butterknife.BindView;
  65. import butterknife.ButterKnife;
  66. import butterknife.Unbinder;
  67. import retrofit2.Call;
  68. import retrofit2.Response;
  69.  
  70. /**
  71.  * Created by anubhava on 26/03/18.
  72.  */
  73.  
  74. public class MapFragment extends Fragment implements MSTCentralManagerIndoorOnlyListener {
  75.  
  76.     public static final String TAG = MapFragment.class.getSimpleName();
  77.     private static final int PERMISSION_REQUEST_FINE_LOCATION = 1;
  78.     private static final String SDK_TOKEN = "PWbWb3Sl407lHdGK3ziNpQSeFyEDK3tX";
  79.     private MainApplication mainApplication;
  80.     private String sdkToken;
  81.     private String floorPlanImageUrl = "";
  82.     private MSTPoint mstPoint = null;
  83.     private boolean addedMap = false;
  84.     private double scaleXFactor;
  85.     private double scaleYFactor;
  86.     private boolean scaleFactorCalled;
  87.     private float floorImageLeftMargin;
  88.     private float floorImageTopMargin;
  89.     public MSTMap currentMap;
  90.     private Unbinder unbinder;
  91.     public HashMap<String, MSTVirtualBeacon> mstVirtualBeaconMap = new HashMap<>();
  92.  
  93.     private final static String TAG_TOKEN = "token";
  94.     SharedPreferences sharedPreferences;
  95.     String token;
  96.  
  97.     public enum AlertType {
  98.         bluetooth,
  99.         network,
  100.         location
  101.     }
  102.  
  103.     @BindView(R.id.floorplan_bluedot)
  104.     FrameLayout floorplanBluedotView;
  105.     @BindView(R.id.floorplan_image)
  106.     ImageView floorPlanImage;
  107.     @BindView(R.id.progress_bar)
  108.     ProgressBar progressBar;
  109.     @BindView(R.id.txt_error)
  110.     TextView txtError;
  111.  
  112.     @BindColor(R.color.black)
  113.     int blackColor;
  114.     @BindColor(R.color.zone_color)
  115.     int zoneColor;
  116.     @BindColor(R.color.vb_color)
  117.     int vbColor;
  118.  
  119.     public static MapFragment newInstance(String sdkToken) {
  120.         Bundle bundle = new Bundle();
  121.         bundle.putString(SDK_TOKEN, sdkToken);
  122.         MapFragment mapFragment = new MapFragment();
  123.         mapFragment.setArguments(bundle);
  124.         return mapFragment;
  125.     }
  126.  
  127.     @Nullable
  128.     @Override
  129.     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  130.                              @Nullable Bundle savedInstanceState) {
  131.         View view = inflater.inflate(R.layout.map_fragment, container, false);
  132.         unbinder = ButterKnife.bind(this, view);
  133.  
  134.         sharedPreferences = getActivity().getSharedPreferences(LoginActivity.my_shared_preferences, Context.MODE_PRIVATE);
  135.         token = sharedPreferences.getString(TAG_TOKEN, "");
  136.  
  137.         //progressBar.setVisibility(View.VISIBLE);
  138.         return view;
  139.     }
  140.  
  141.  
  142.     public void Insertkehadirandosen_ble (String idruang, String token){
  143.  
  144.         ApiService apiService = Server.getClient().create(ApiService.class);
  145.         Call<BleModel> call = apiService.getInsertkehadirandosen_ble(idruang, token);
  146.         call.enqueue(new retrofit2.Callback<BleModel>() {
  147.             @Override
  148.             public void onResponse(Call<BleModel> call, Response<BleModel> response) {
  149.  
  150.                 BleModel model = response.body();
  151.  
  152.                 try {
  153.  
  154.                     if (response.isSuccessful()) {
  155.  
  156.                         System.out.println("MODEL : "+model.getMsg());
  157. //                        Toast.makeText(PresensiQRActivity.this, ""+model.getMsg(), Toast.LENGTH_SHORT).show();
  158.                         final Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
  159.                                 model.getMsg(), Snackbar.LENGTH_INDEFINITE);
  160.                         snackBar.setAction("Dismiss", new View.OnClickListener() {
  161.                             @Override
  162.                             public void onClick(View v) {
  163.                                 android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(getContext()).create();
  164.                                 alertDialog.setIcon(R.drawable.ic_sentiment_dissatisfied);
  165.                                 alertDialog.setTitle("Presensi");
  166.                                 alertDialog.setMessage("Anda telah sukses melakukan presensi");
  167.                                 alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
  168.                                         new DialogInterface.OnClickListener() {
  169.                                             public void onClick(DialogInterface dialog, int which) {
  170.                                                 dialog.dismiss();
  171.                                             }
  172.                                         });
  173.                                 alertDialog.show();
  174.                             }
  175.                         });
  176.                         View snackBarView = snackBar.getView();
  177.                         TextView textView = snackBarView.findViewById(android.support.design.R.id.snackbar_text);
  178.                         textView.setTextColor(blackColor);
  179.                         snackBar.show();
  180.  
  181.  
  182.                     }
  183.  
  184.                 } catch (Exception e) {
  185.                     e.printStackTrace();
  186.                 }
  187.             }
  188.  
  189.             @Override
  190.             public void onFailure(Call<BleModel> call, Throwable t) {
  191.                 Toast.makeText(getContext(), "Kesalahan Jaringan", Toast.LENGTH_SHORT).show();
  192.             }
  193.         });
  194.  
  195.     }
  196.  
  197.     @Override
  198.     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  199.         super.onViewCreated(view, savedInstanceState);
  200.         if (getActivity() != null)
  201.             mainApplication = (MainApplication) getActivity().getApplication();
  202.         if (getArguments() != null)
  203.             sdkToken = getArguments().getString(SDK_TOKEN);
  204.     }
  205.  
  206.     @Override
  207.     public void onStart() {
  208.         super.onStart();
  209.         initMISTSDK();
  210.     }
  211.  
  212.     private void initMISTSDK() {
  213.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity() != null &&
  214.                 getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
  215.                         != PackageManager.PERMISSION_GRANTED) {
  216.             showLocationPermissionDialog();
  217.         } else {
  218.             startMistSdk();
  219.         }
  220.     }
  221.  
  222.     //permission dialogs
  223.     private void showLocationPermissionDialog() {
  224.         if (getActivity() != null) {
  225.             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  226.             builder.setTitle("This app needs location access");
  227.             builder.setMessage("Please grant location access so this app can detect beacons in the background.");
  228.             builder.setPositiveButton(android.R.string.ok, null);
  229.             builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  230.                 @Override
  231.                 public void onDismiss(DialogInterface dialog) {
  232.                     requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  233.                             PERMISSION_REQUEST_FINE_LOCATION);
  234.                 }
  235.             });
  236.             builder.show();
  237.         }
  238.     }
  239.  
  240.     @Override
  241.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  242.                                            @NonNull int[] grantResults) {
  243.         if (getActivity() != null) {
  244.             switch (requestCode) {
  245.                 case PERMISSION_REQUEST_FINE_LOCATION:
  246.                     if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  247.                         Log.d(TAG, "fine location permission granted !!");
  248.                         startMistSdk();
  249.                     } else {
  250.                         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  251.                         builder.setTitle("Functionality limited");
  252.                         builder.setMessage("Since location access has not been granted, " +
  253.                                 "this app will not be able to discover beacons when in the background.");
  254.                         builder.setPositiveButton(android.R.string.ok, null);
  255.                         builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  256.                             @Override
  257.                             public void onDismiss(DialogInterface dialog) {
  258.                             }
  259.                         });
  260.                         builder.show();
  261.                     }
  262.             }
  263.         }
  264.     }
  265.  
  266.     /**
  267.      * This method checks for the availability for Internet , Location and Bluetooth and show dialog if anything is not enabled else start the Mist SDK
  268.      */
  269.     private void startMistSdk() {
  270.         BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  271.         if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && getActivity() != null &&
  272.                 Utils.isNetworkAvailable(getActivity()) && Utils.isLocationServiceEnabled(getActivity())) {
  273.             runMISTSDK();
  274.         } else {
  275.             if (getActivity() != null && !Utils.isNetworkAvailable(getActivity())) {
  276.                 showSettingsAlert(AlertType.network);
  277.             }
  278.             if (getActivity() != null && !Utils.isLocationServiceEnabled(getActivity())) {
  279.                 showSettingsAlert(AlertType.location);
  280.             }
  281.             if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
  282.                 showSettingsAlert(AlertType.bluetooth);
  283.             }
  284.         }
  285.     }
  286.  
  287.     //initializing the Mist sdk with sdkToken
  288.     private void runMISTSDK() {
  289.         MistManager mistManager = MistManager.newInstance(mainApplication);
  290.         mistManager.init(sdkToken, this, AppMode.FOREGROUND);
  291.     }
  292.  
  293.     /**
  294.      * This method show the alert as per AlertType
  295.      *
  296.      * @param alertType Type of Alert
  297.      *                  bluetooth
  298.      *                  network
  299.      *                  location
  300.      */
  301.     private void showSettingsAlert(final AlertType alertType) {
  302.         if (getActivity() != null) {
  303.             final String sTitle, sButton;
  304.             if (alertType == AlertType.bluetooth) {
  305.                 sTitle = "Bluetooth is disabled in your device. Would you like to enable it?";
  306.                 sButton = "Goto Settings Page To Enable Bluetooth";
  307.             } else if (alertType == AlertType.network) {
  308.                 sTitle = "Network Connection is disabled in your device. Would you like to enable it?";
  309.                 sButton = "Goto Settings Page To Enable Network Connection";
  310.             } else {
  311.                 sTitle = "Location is disabled in your device. Would you like to enable it?";
  312.                 sButton = "Goto Settings Page To Enable Location";
  313.             }
  314.  
  315.             AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
  316.             alertDialogBuilder.setMessage(sTitle)
  317.                     .setCancelable(false)
  318.                     .setPositiveButton(sButton,
  319.                             new DialogInterface.OnClickListener() {
  320.                                 public void onClick(DialogInterface dialog, int id) {
  321.                                     dialog.cancel();
  322.                                     Intent intentOpenBluetoothSettings = new Intent();
  323.                                     if (alertType == AlertType.bluetooth) {
  324.                                         intentOpenBluetoothSettings.setAction(Settings.ACTION_BLUETOOTH_SETTINGS);
  325.                                     } else if (alertType == AlertType.network) {
  326.                                         intentOpenBluetoothSettings.setAction(Settings.ACTION_WIFI_SETTINGS);
  327.                                     } else if (alertType == AlertType.location) {
  328.                                         intentOpenBluetoothSettings.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  329.                                     }
  330.  
  331.                                     startActivity(intentOpenBluetoothSettings);
  332.                                 }
  333.                             });
  334.             alertDialogBuilder.setNegativeButton("Cancel",
  335.                     new DialogInterface.OnClickListener() {
  336.                         public void onClick(DialogInterface dialog, int id) {
  337.                             dialog.cancel();
  338.                             final AlertDialog.Builder builder = new
  339.                                     AlertDialog.Builder(getActivity());
  340.                             builder.setTitle("Functionality won't work");
  341.                             builder.setMessage(sButton);
  342.                             builder.setPositiveButton(android.R.string.ok, null);
  343.                             builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  344.                                 @Override
  345.                                 public void onDismiss(DialogInterface dialog) {
  346.                                 }
  347.                             });
  348.                             builder.show();
  349.                         }
  350.                     });
  351.             AlertDialog alert = alertDialogBuilder.create();
  352.             alert.show();
  353.         }
  354.     }
  355.  
  356.     @Override
  357.     public void onBeaconDetected(MSTBeacon[] beaconArray, String region, Date dateUpdated) {
  358.  
  359.     }
  360.  
  361.     @Override
  362.     public void onBeaconDetected(JSONArray beaconArray, Date dateUpdated) {
  363.  
  364.     }
  365.  
  366.     @Override
  367.     public void onBeaconListUpdated(HashMap<String, HashMap<Integer, Integer[]>> beaconList, Date dateUpdated) {
  368.  
  369.     }
  370.  
  371.     /**
  372.      * This callback provide the location of the device
  373.      *
  374.      * @param relativeLocation provide x,y of the device on particular map
  375.      * @param maps
  376.      * @param dateUpdated      time stamp of the location provided
  377.      */
  378.     @Override
  379.     public void onRelativeLocationUpdated(MSTPoint relativeLocation, MSTMap[] maps, Date dateUpdated) {
  380.         if (relativeLocation != null && maps != null) {
  381.             mstPoint = relativeLocation;
  382.             updateRelativeLocation();
  383.         }
  384.     }
  385.  
  386.     private void updateRelativeLocation() {
  387.         if (getActivity() != null) {
  388.             getActivity().runOnUiThread(new Runnable() {
  389.                 @Override
  390.                 public void run() {
  391.                     if (currentMap != null && addedMap) {
  392.                         //(mstPoint);
  393.                         //Toast.makeText(getContext(), "WIS GA ATEK MAP2an", Toast.LENGTH_LONG).show();
  394.                     }
  395.                 }
  396.             });
  397.         }
  398.     }
  399.  
  400.     //logic to show the blue dot for the location
  401.     public void renderBlueDot(final MSTPoint point) {
  402.         if (getActivity() != null) {
  403.             getActivity().runOnUiThread(new Runnable() {
  404.                 @Override
  405.                 public void run() {
  406.                     if (floorPlanImage != null && floorPlanImage.getDrawable() != null && currentMap != null && point != null && addedMap) {
  407.                         float xPos = convertCloudPointToFloorplanXScale(point.getX());
  408.                         float yPos = convertCloudPointToFloorplanYScale(point.getY());
  409.  
  410.                         // If scaleX and scaleY are not defined, check again
  411.                         if (!scaleFactorCalled && (scaleXFactor == 0 || scaleYFactor == 0)) {
  412. //                            setupScaleFactorForFloorplan();
  413.                         }
  414.                         float leftMargin = floorImageLeftMargin + (xPos - (floorplanBluedotView.getWidth() / 2));
  415.                         float topMargin = floorImageTopMargin + (yPos - (floorplanBluedotView.getHeight() / 2));
  416.  
  417.                         floorplanBluedotView.setX(leftMargin);
  418.                         floorplanBluedotView.setY(topMargin);
  419.                         floorplanBluedotView.setVisibility(View.VISIBLE);
  420.                     }
  421.                 }
  422.             });
  423.         }
  424.     }
  425.  
  426.  
  427.     //calculating the scale factors
  428.     private void setupScaleFactorForFloorplan() {
  429.         if (floorPlanImage != null) {
  430.             ViewTreeObserver vto = floorPlanImage.getViewTreeObserver();
  431.             vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  432.                 @Override
  433.                 public void onGlobalLayout() {
  434.                     if (floorPlanImage != null) {
  435.                         floorImageLeftMargin = floorPlanImage.getLeft();
  436.                         floorImageTopMargin = floorPlanImage.getTop();
  437.                         if (floorPlanImage.getDrawable() != null) {
  438.                             scaleXFactor = (floorPlanImage.getWidth() / (double) floorPlanImage.getDrawable().getIntrinsicWidth());
  439.                             scaleYFactor = (floorPlanImage.getHeight() / (double) floorPlanImage.getDrawable().getIntrinsicHeight());
  440.                             scaleFactorCalled = true;
  441.                         }
  442.                     }
  443.                 }
  444.             });
  445.         }
  446.     }
  447.  
  448.     //converting the x point from meter's to pixel with the present scaling factor of the map rendered in the imageview
  449.     private float convertCloudPointToFloorplanXScale(double meter) {
  450.         return (float) (meter * this.scaleXFactor * currentMap.getPpm());
  451.     }
  452.  
  453.     //converting the y point from meter's to pixel with the present scaling factor of the map rendered in the imageview
  454.     private float convertCloudPointToFloorplanYScale(double meter) {
  455.         return (float) (meter * this.scaleYFactor * currentMap.getPpm());
  456.     }
  457.  
  458.     @Override
  459.     public void onPressureUpdated(double pressure, Date dateUpdated) {
  460.  
  461.     }
  462.  
  463.     @Override
  464.     public void onZoneStatsUpdated(MSTZone[] zones, Date dateUpdated) {
  465.  
  466.     }
  467.  
  468.     @Override
  469.     public void onClientUpdated(MSTClient[] clients, MSTZone[] zones, Date dateUpdated) {
  470.  
  471.     }
  472.  
  473.     @Override
  474.     public void onAssetUpdated(MSTAsset[] assets, MSTZone[] zones, Date dateUpdated) {
  475.  
  476.     }
  477.  
  478.     /**
  479.      * This callback provide the detail of map user is on
  480.      *
  481.      * @param map         Map object having details about the map
  482.      * @param dateUpdated
  483.      */
  484.     @Override
  485.     public void onMapUpdated(MSTMap map, Date dateUpdated) {
  486. //        floorPlanImageUrl = map.getMapImageUrl();
  487. //        Log.d(TAG, floorPlanImageUrl);
  488. //        if (getActivity() != null && (floorPlanImage.getDrawable() == null || this.currentMap == null || !this.currentMap.getMapId().equals(map.getMapId()))) {
  489. //            // Set the current map
  490. //            this.currentMap = map;
  491. //            getActivity().runOnUiThread(new Runnable() {
  492. //                @Override
  493. //                public void run() {
  494. //                    renderImage(floorPlanImageUrl);
  495. //                }
  496. //            });
  497. //        }
  498.     }
  499.  
  500.     /**
  501.      * This method is used for rendering the map image using the url from the MSTMap object received from OnMapUpdated callback
  502.      *
  503.      * @param floorPlanImageUrl map image url
  504.      */
  505.     private void renderImage(final String floorPlanImageUrl) {
  506.         Log.d(TAG, "in picasso");
  507.         addedMap = false;
  508.         Picasso.with(getActivity()).
  509.                 load(floorPlanImageUrl).
  510.                 networkPolicy(NetworkPolicy.OFFLINE).
  511.                 into(floorPlanImage, new Callback() {
  512.                     @Override
  513.                     public void onSuccess() {
  514.                         Log.d(TAG, "Image loaded successfully from the cached");
  515.                         addedMap = true;
  516.                         progressBar.setVisibility(View.GONE);
  517.  
  518.                         if (!scaleFactorCalled) {
  519.                             setupScaleFactorForFloorplan();
  520.                         }
  521.                     }
  522.  
  523.                     @Override
  524.                     public void onError() {
  525.                         Picasso.with(getActivity()).
  526.                                 load(floorPlanImageUrl)
  527.                                 .into(floorPlanImage, new Callback() {
  528.                                     @Override
  529.                                     public void onSuccess() {
  530.                                         progressBar.setVisibility(View.GONE);
  531.                                         addedMap = true;
  532.                                         if (!scaleFactorCalled) {
  533.                                             setupScaleFactorForFloorplan();
  534.                                         }
  535.                                         Log.d(TAG, "Image downloaded from server successfully !!");
  536.                                     }
  537.  
  538.                                     @Override
  539.                                     public void onError() {
  540.                                         progressBar.setVisibility(View.GONE);
  541.                                         Log.d(TAG, "Could not download the image from the server");
  542.                                     }
  543.                                 });
  544.                     }
  545.                 });
  546.     }
  547.  
  548.     //provides the list of vBeacons atatched to your floor plan
  549.     @Override
  550.     public void onVirtualBeaconListUpdated(MSTVirtualBeacon[] virtualBeacons, Date dateUpdated) {
  551.         mstVirtualBeaconMap.clear();
  552.         for (MSTVirtualBeacon vb : virtualBeacons) {
  553.             mstVirtualBeaconMap.put(vb.getVbid(), vb);
  554.         }
  555.     }
  556.  
  557.     //called when notifications are received on entering a zone or passing by a vBecaon
  558.     @Override
  559.     public void onNotificationReceived(Date dateReceived, String message) {
  560.         Log.d(TAG, "notification recieved!!");
  561.         Log.d(TAG, "Message => " + message);
  562.         if (!Utils.isEmptyString(message)) {
  563.             try {
  564.                 JSONObject notificationJSONObject = new JSONObject(message);
  565.                 String type = notificationJSONObject.getString("type");
  566.                 if (type.equalsIgnoreCase("zone-event-vb")) {
  567.                     JSONObject messageObject = notificationJSONObject.optJSONObject("message");
  568.                     if (messageObject != null) {
  569.                         String proximity = messageObject.getString("proximity");
  570.                         if (proximity.equals("near") || proximity.equals("immediate")) {
  571.                             String messageToBeDisplayed = "";
  572.                             String extra = messageObject.getString("Extra");
  573.                             String vbID = messageObject.optString("vbID");
  574.                             if (mstVirtualBeaconMap.containsKey(vbID)) {
  575.                                 MSTVirtualBeacon vb = mstVirtualBeaconMap.get(vbID);
  576.                                 messageToBeDisplayed = vb.getMessage();
  577.                             }
  578.                             if (TextUtils.isEmpty(messageToBeDisplayed)) {
  579.                                 if (TextUtils.isEmpty(extra)) {
  580.                                     messageToBeDisplayed = "You're near the Anonymous VB";
  581.                                 } else {
  582.                                     if (proximity.equals("near")) {
  583.                                         String dekat = "dekat dengan";
  584.                                         messageToBeDisplayed = String.format("Anda %1$s %2$s", dekat, extra);
  585.                                     }
  586.                                 }
  587.                             }
  588.                             showNotification(false, messageToBeDisplayed);
  589.                         } else if (proximity.equals("far")) {
  590.                             String messageToBeDisplayed = "";
  591.                             String extra = messageObject.getString("Extra");
  592.                             String vbID = messageObject.optString("vbID");
  593.                             if (mstVirtualBeaconMap.containsKey(vbID)) {
  594.                                 MSTVirtualBeacon vb = mstVirtualBeaconMap.get(vbID);
  595.                                 messageToBeDisplayed = vb.getMessage();
  596.                             }
  597.                             if (TextUtils.isEmpty(messageToBeDisplayed)) {
  598.                                 if (TextUtils.isEmpty(extra)) {
  599.                                     messageToBeDisplayed = "You're far from the Anonymous VB";
  600.                                 } else {
  601.                                     messageToBeDisplayed = String.format("You're %1$s %2$s", proximity, extra);
  602.                                 }
  603.                             }
  604.                             //action can be taken according to the need in case of far beacon
  605.                         }
  606.                     }
  607.  
  608.                 } else if (type.equalsIgnoreCase("zones-events")) {
  609.                     JSONObject messageObject = notificationJSONObject.optJSONObject("message");
  610.                     if (messageObject != null) {
  611.                         String trigger = messageObject.getString("Trigger");
  612.                         if (trigger.equalsIgnoreCase("in")) {
  613.                             String messageToBeDisplayed = "";
  614.                             String extra = messageObject.getString("Extra");
  615.  
  616.                            //Insertkehadirandosen_ble( extra,token);//sini
  617.                             Insertkehadirandosen_ble("B34BE141-7FB5" +
  618.                                     "-457B-B8E9-C2BE4806CACC",token);//sini
  619.  
  620.  
  621.                             if (TextUtils.isEmpty(extra)) {
  622.                                 messageToBeDisplayed = "You're in the Anonymous Zone";
  623.                             } else {
  624.                                 messageToBeDisplayed = String.format("Anda sedang berada di %1$s %2$s", trigger, extra);
  625.                             }
  626.                             showNotification(true, messageToBeDisplayed);
  627.                         }
  628.                         if (trigger.equalsIgnoreCase("out")) {
  629.                             String messageToBeDisplayed = "";
  630.                             String extra = messageObject.getString("Extra");
  631.                             if (TextUtils.isEmpty(extra)) {
  632.                                 messageToBeDisplayed = "You left the Anonymous Zone";
  633.                             } else {
  634.                                 messageToBeDisplayed = String.format("Anda sedang meninggalkan %1$s", extra);
  635.                             }
  636.                             showNotification(true, messageToBeDisplayed);
  637.                         }
  638.                     }
  639.                 }
  640.             } catch (JSONException e) {
  641.                 e.printStackTrace();
  642.             }
  643.         }
  644.  
  645.         try {
  646.             JSONObject jsonObject = new JSONObject(message);
  647.             JSONObject msgObj = jsonObject.getJSONObject("message");
  648.             String UserID = msgObj.getString("UserID");
  649.  
  650.             Log.e("TAG", "UserID ==> " + UserID);
  651.         } catch (JSONException e) {
  652.             e.printStackTrace();
  653.         }
  654.     }
  655.  
  656.     //showing the notifications
  657.     private void showNotification(boolean isZone, String message) {
  658.         final Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
  659.                 message, Snackbar.LENGTH_INDEFINITE);
  660.         snackBar.setAction("Dismiss", new View.OnClickListener() {
  661.             @Override
  662.             public void onClick(View v) {
  663.                 android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(getContext()).create();
  664.                 alertDialog.setIcon(R.drawable.ic_sentiment_dissatisfied);
  665.                 alertDialog.setTitle("Presensi");
  666.                 alertDialog.setMessage("Anda telah sukses melakukan presensi");
  667.                 alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
  668.                         new DialogInterface.OnClickListener() {
  669.                             public void onClick(DialogInterface dialog, int which) {
  670.                                 dialog.dismiss();
  671.                             }
  672.                         });
  673.                 alertDialog.show();
  674.             }
  675.         });
  676.         View snackBarView = snackBar.getView();
  677.         TextView textView = snackBarView.findViewById(android.support.design.R.id.snackbar_text);
  678.         textView.setTextColor(blackColor);
  679.         if (isZone) {
  680.             snackBarView.setBackgroundColor(zoneColor);
  681.         } else {
  682.             snackBarView.setBackgroundColor(vbColor);
  683.         }
  684.         snackBar.show();
  685.     }
  686.  
  687.     @Override
  688.     public void onClientInformationUpdated(String clientName) {
  689.  
  690.     }
  691.  
  692.     @Override
  693.     public void onReceivedSecret(String orgName, String orgID, String sdkSecret, String error) {
  694.  
  695.     }
  696.  
  697.     @Override
  698.     public void receivedLogMessageForCode(String message, MSTCentralManagerStatusCode code) {
  699.     }
  700.  
  701.     @Override
  702.     public void receivedVerboseLogMessage(String message) {
  703.     }
  704.  
  705.     //callback for error
  706.     @Override
  707.     public void onMistErrorReceived(String message, Date date) {
  708.         progressBar.setVisibility(View.GONE);
  709.         txtError.setVisibility(View.VISIBLE);
  710.         txtError.setText(message);
  711.     }
  712.  
  713.     @Override
  714.     public void onMistRecommendedAction(String message) {
  715.  
  716.     }
  717.  
  718.     @Override
  719.     public void onDestroyView() {
  720.         super.onDestroyView();
  721.         unbinder.unbind();
  722.         MistManager.newInstance(mainApplication).destroy();
  723.     }
  724.  
  725.     @Override
  726.     public void onStop() {
  727.         super.onStop();
  728.         //disconnecting the Mist SDK
  729.         MistManager.newInstance(mainApplication).disconnect();
  730.     }
  731. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement