Advertisement
ekohd

apiservice

Jun 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 30.76 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.  
  98.     public enum AlertType {
  99.         bluetooth,
  100.         network,
  101.         location
  102.     }
  103.  
  104.     @BindView(R.id.floorplan_bluedot)
  105.     FrameLayout floorplanBluedotView;
  106.     @BindView(R.id.floorplan_image)
  107.     ImageView floorPlanImage;
  108.     @BindView(R.id.progress_bar)
  109.     ProgressBar progressBar;
  110.     @BindView(R.id.txt_error)
  111.     TextView txtError;
  112.  
  113.     @BindColor(R.color.black)
  114.     int blackColor;
  115.     @BindColor(R.color.zone_color)
  116.     int zoneColor;
  117.     @BindColor(R.color.vb_color)
  118.     int vbColor;
  119.  
  120.     public static MapFragment newInstance(String sdkToken) {
  121.         Bundle bundle = new Bundle();
  122.         bundle.putString(SDK_TOKEN, sdkToken);
  123.         MapFragment mapFragment = new MapFragment();
  124.         mapFragment.setArguments(bundle);
  125.         return mapFragment;
  126.     }
  127.  
  128.     @Nullable
  129.     @Override
  130.     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  131.                              @Nullable Bundle savedInstanceState) {
  132.         View view = inflater.inflate(R.layout.map_fragment, container, false);
  133.         unbinder = ButterKnife.bind(this, view);
  134.  
  135.         sharedPreferences = getActivity().getSharedPreferences(LoginActivity.my_shared_preferences, Context.MODE_PRIVATE);
  136.         token = sharedPreferences.getString(TAG_TOKEN, "");
  137.  
  138.         System.out.println("TOKEN : "+token);
  139.  
  140.  
  141.         //progressBar.setVisibility(View.VISIBLE);
  142.         return view;
  143.     }
  144.  
  145.     public void Insertkehadirandosen_ble (String idruang, String token){
  146.  
  147.         ApiService apiService = Server.getClient().create(ApiService.class);
  148.         Call<BleModel> call = apiService.getInsertkehadirandosen_ble(idruang, token);
  149.         call.enqueue(new retrofit2.Callback<BleModel>() {
  150.             @Override
  151.             public void onResponse(Call<BleModel> call, Response<BleModel> response) {
  152.  
  153.                 BleModel model = response.body();
  154.  
  155.                 try {
  156.  
  157.                     if (response.isSuccessful()) {
  158.  
  159.                         System.out.println("MODEL : "+model.getMsg());
  160. //                        Toast.makeText(PresensiQRActivity.this, ""+model.getMsg(), Toast.LENGTH_SHORT).show();
  161.                         final Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
  162.                                 model.getMsg(), Snackbar.LENGTH_INDEFINITE);
  163.                         snackBar.setAction("Dismiss", new View.OnClickListener() {
  164.                             @Override
  165.                             public void onClick(View v) {
  166.                                 android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(getContext()).create();
  167.                                 alertDialog.setIcon(R.drawable.ic_sentiment_dissatisfied);
  168.                                 alertDialog.setTitle("Presensi");
  169.                                 alertDialog.setMessage("Anda telah sukses melakukan presensi");
  170.                                 alertDialog.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, "OK",
  171.                                         new DialogInterface.OnClickListener() {
  172.                                             public void onClick(DialogInterface dialog, int which) {
  173.                                                 dialog.dismiss();
  174.                                             }
  175.                                         });
  176.                                 alertDialog.show();
  177.                             }
  178.                         });
  179.                         View snackBarView = snackBar.getView();
  180.                         TextView textView = snackBarView.findViewById(android.support.design.R.id.snackbar_text);
  181.                         textView.setTextColor(blackColor);
  182.                         snackBar.show();
  183.  
  184.  
  185.                     }
  186.  
  187.                 } catch (Exception e) {
  188.                     e.printStackTrace();
  189.                 }
  190.             }
  191.  
  192.             @Override
  193.             public void onFailure(Call<BleModel> call, Throwable t) {
  194.                 Toast.makeText(getContext(), "Kesalahan Jaringan", Toast.LENGTH_SHORT).show();
  195.             }
  196.         });
  197.  
  198.     }
  199.  
  200.     @Override
  201.     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  202.         super.onViewCreated(view, savedInstanceState);
  203.         if (getActivity() != null)
  204.             mainApplication = (MainApplication) getActivity().getApplication();
  205.         if (getArguments() != null)
  206.             sdkToken = getArguments().getString(SDK_TOKEN);
  207.     }
  208.  
  209.     @Override
  210.     public void onStart() {
  211.         super.onStart();
  212.         initMISTSDK();
  213.     }
  214.  
  215.     private void initMISTSDK() {
  216.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity() != null &&
  217.                 getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
  218.                         != PackageManager.PERMISSION_GRANTED) {
  219.             showLocationPermissionDialog();
  220.         } else {
  221.             startMistSdk();
  222.         }
  223.     }
  224.  
  225.     //permission dialogs
  226.     private void showLocationPermissionDialog() {
  227.         if (getActivity() != null) {
  228.             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  229.             builder.setTitle("This app needs location access");
  230.             builder.setMessage("Please grant location access so this app can detect beacons in the background.");
  231.             builder.setPositiveButton(android.R.string.ok, null);
  232.             builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  233.                 @Override
  234.                 public void onDismiss(DialogInterface dialog) {
  235.                     requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  236.                             PERMISSION_REQUEST_FINE_LOCATION);
  237.                 }
  238.             });
  239.             builder.show();
  240.         }
  241.     }
  242.  
  243.     @Override
  244.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  245.                                            @NonNull int[] grantResults) {
  246.         if (getActivity() != null) {
  247.             switch (requestCode) {
  248.                 case PERMISSION_REQUEST_FINE_LOCATION:
  249.                     if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  250.                         Log.d(TAG, "fine location permission granted !!");
  251.                         startMistSdk();
  252.                     } else {
  253.                         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  254.                         builder.setTitle("Functionality limited");
  255.                         builder.setMessage("Since location access has not been granted, " +
  256.                                 "this app will not be able to discover beacons when in the background.");
  257.                         builder.setPositiveButton(android.R.string.ok, null);
  258.                         builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  259.                             @Override
  260.                             public void onDismiss(DialogInterface dialog) {
  261.                             }
  262.                         });
  263.                         builder.show();
  264.                     }
  265.             }
  266.         }
  267.     }
  268.  
  269.     /**
  270.      * This method checks for the availability for Internet , Location and Bluetooth and show dialog if anything is not enabled else start the Mist SDK
  271.      */
  272.     private void startMistSdk() {
  273.         BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  274.         if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && getActivity() != null &&
  275.                 Utils.isNetworkAvailable(getActivity()) && Utils.isLocationServiceEnabled(getActivity())) {
  276.             runMISTSDK();
  277.         } else {
  278.             if (getActivity() != null && !Utils.isNetworkAvailable(getActivity())) {
  279.                 showSettingsAlert(AlertType.network);
  280.             }
  281.             if (getActivity() != null && !Utils.isLocationServiceEnabled(getActivity())) {
  282.                 showSettingsAlert(AlertType.location);
  283.             }
  284.             if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
  285.                 showSettingsAlert(AlertType.bluetooth);
  286.             }
  287.         }
  288.     }
  289.  
  290.     //initializing the Mist sdk with sdkToken
  291.     private void runMISTSDK() {
  292.         MistManager mistManager = MistManager.newInstance(mainApplication);
  293.         mistManager.init(sdkToken, this, AppMode.FOREGROUND);
  294.     }
  295.  
  296.     /**
  297.      * This method show the alert as per AlertType
  298.      *
  299.      * @param alertType Type of Alert
  300.      *                  bluetooth
  301.      *                  network
  302.      *                  location
  303.      */
  304.     private void showSettingsAlert(final AlertType alertType) {
  305.         if (getActivity() != null) {
  306.             final String sTitle, sButton;
  307.             if (alertType == AlertType.bluetooth) {
  308.                 sTitle = "Bluetooth is disabled in your device. Would you like to enable it?";
  309.                 sButton = "Goto Settings Page To Enable Bluetooth";
  310.             } else if (alertType == AlertType.network) {
  311.                 sTitle = "Network Connection is disabled in your device. Would you like to enable it?";
  312.                 sButton = "Goto Settings Page To Enable Network Connection";
  313.             } else {
  314.                 sTitle = "Location is disabled in your device. Would you like to enable it?";
  315.                 sButton = "Goto Settings Page To Enable Location";
  316.             }
  317.  
  318.             AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
  319.             alertDialogBuilder.setMessage(sTitle)
  320.                     .setCancelable(false)
  321.                     .setPositiveButton(sButton,
  322.                             new DialogInterface.OnClickListener() {
  323.                                 public void onClick(DialogInterface dialog, int id) {
  324.                                     dialog.cancel();
  325.                                     Intent intentOpenBluetoothSettings = new Intent();
  326.                                     if (alertType == AlertType.bluetooth) {
  327.                                         intentOpenBluetoothSettings.setAction(Settings.ACTION_BLUETOOTH_SETTINGS);
  328.                                     } else if (alertType == AlertType.network) {
  329.                                         intentOpenBluetoothSettings.setAction(Settings.ACTION_WIFI_SETTINGS);
  330.                                     } else if (alertType == AlertType.location) {
  331.                                         intentOpenBluetoothSettings.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  332.                                     }
  333.  
  334.                                     startActivity(intentOpenBluetoothSettings);
  335.                                 }
  336.                             });
  337.             alertDialogBuilder.setNegativeButton("Cancel",
  338.                     new DialogInterface.OnClickListener() {
  339.                         public void onClick(DialogInterface dialog, int id) {
  340.                             dialog.cancel();
  341.                             final AlertDialog.Builder builder = new
  342.                                     AlertDialog.Builder(getActivity());
  343.                             builder.setTitle("Functionality won't work");
  344.                             builder.setMessage(sButton);
  345.                             builder.setPositiveButton(android.R.string.ok, null);
  346.                             builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
  347.                                 @Override
  348.                                 public void onDismiss(DialogInterface dialog) {
  349.                                 }
  350.                             });
  351.                             builder.show();
  352.                         }
  353.                     });
  354.             AlertDialog alert = alertDialogBuilder.create();
  355.             alert.show();
  356.         }
  357.     }
  358.  
  359.     @Override
  360.     public void onBeaconDetected(MSTBeacon[] beaconArray, String region, Date dateUpdated) {
  361.  
  362.     }
  363.  
  364.     @Override
  365.     public void onBeaconDetected(JSONArray beaconArray, Date dateUpdated) {
  366.  
  367.     }
  368.  
  369.     @Override
  370.     public void onBeaconListUpdated(HashMap<String, HashMap<Integer, Integer[]>> beaconList, Date dateUpdated) {
  371.  
  372.     }
  373.  
  374.     /**
  375.      * This callback provide the location of the device
  376.      *
  377.      * @param relativeLocation provide x,y of the device on particular map
  378.      * @param maps
  379.      * @param dateUpdated      time stamp of the location provided
  380.      */
  381.     @Override
  382.     public void onRelativeLocationUpdated(MSTPoint relativeLocation, MSTMap[] maps, Date dateUpdated) {
  383.         if (relativeLocation != null && maps != null) {
  384.             mstPoint = relativeLocation;
  385.             updateRelativeLocation();
  386.         }
  387.     }
  388.  
  389.     private void updateRelativeLocation() {
  390.         if (getActivity() != null) {
  391.             getActivity().runOnUiThread(new Runnable() {
  392.                 @Override
  393.                 public void run() {
  394.                     if (currentMap != null && addedMap) {
  395.                         //(mstPoint);
  396.                         //Toast.makeText(getContext(), "WIS GA ATEK MAP2an", Toast.LENGTH_LONG).show();
  397.                     }
  398.                 }
  399.             });
  400.         }
  401.     }
  402.  
  403.     //logic to show the blue dot for the location
  404.     public void renderBlueDot(final MSTPoint point) {
  405.         if (getActivity() != null) {
  406.             getActivity().runOnUiThread(new Runnable() {
  407.                 @Override
  408.                 public void run() {
  409.                     if (floorPlanImage != null && floorPlanImage.getDrawable() != null && currentMap != null && point != null && addedMap) {
  410.                         float xPos = convertCloudPointToFloorplanXScale(point.getX());
  411.                         float yPos = convertCloudPointToFloorplanYScale(point.getY());
  412.  
  413.                         // If scaleX and scaleY are not defined, check again
  414.                         if (!scaleFactorCalled && (scaleXFactor == 0 || scaleYFactor == 0)) {
  415. //                            setupScaleFactorForFloorplan();
  416.                         }
  417.                         float leftMargin = floorImageLeftMargin + (xPos - (floorplanBluedotView.getWidth() / 2));
  418.                         float topMargin = floorImageTopMargin + (yPos - (floorplanBluedotView.getHeight() / 2));
  419.  
  420.                         floorplanBluedotView.setX(leftMargin);
  421.                         floorplanBluedotView.setY(topMargin);
  422.                         floorplanBluedotView.setVisibility(View.VISIBLE);
  423.                     }
  424.                 }
  425.             });
  426.         }
  427.     }
  428.  
  429.  
  430.     //calculating the scale factors
  431.     private void setupScaleFactorForFloorplan() {
  432.         if (floorPlanImage != null) {
  433.             ViewTreeObserver vto = floorPlanImage.getViewTreeObserver();
  434.             vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  435.                 @Override
  436.                 public void onGlobalLayout() {
  437.                     if (floorPlanImage != null) {
  438.                         floorImageLeftMargin = floorPlanImage.getLeft();
  439.                         floorImageTopMargin = floorPlanImage.getTop();
  440.                         if (floorPlanImage.getDrawable() != null) {
  441.                             scaleXFactor = (floorPlanImage.getWidth() / (double) floorPlanImage.getDrawable().getIntrinsicWidth());
  442.                             scaleYFactor = (floorPlanImage.getHeight() / (double) floorPlanImage.getDrawable().getIntrinsicHeight());
  443.                             scaleFactorCalled = true;
  444.                         }
  445.                     }
  446.                 }
  447.             });
  448.         }
  449.     }
  450.  
  451.     //converting the x point from meter's to pixel with the present scaling factor of the map rendered in the imageview
  452.     private float convertCloudPointToFloorplanXScale(double meter) {
  453.         return (float) (meter * this.scaleXFactor * currentMap.getPpm());
  454.     }
  455.  
  456.     //converting the y point from meter's to pixel with the present scaling factor of the map rendered in the imageview
  457.     private float convertCloudPointToFloorplanYScale(double meter) {
  458.         return (float) (meter * this.scaleYFactor * currentMap.getPpm());
  459.     }
  460.  
  461.     @Override
  462.     public void onPressureUpdated(double pressure, Date dateUpdated) {
  463.  
  464.     }
  465.  
  466.     @Override
  467.     public void onZoneStatsUpdated(MSTZone[] zones, Date dateUpdated) {
  468.  
  469.     }
  470.  
  471.     @Override
  472.     public void onClientUpdated(MSTClient[] clients, MSTZone[] zones, Date dateUpdated) {
  473.  
  474.     }
  475.  
  476.     @Override
  477.     public void onAssetUpdated(MSTAsset[] assets, MSTZone[] zones, Date dateUpdated) {
  478.  
  479.     }
  480.  
  481.     /**
  482.      * This callback provide the detail of map user is on
  483.      *
  484.      * @param map         Map object having details about the map
  485.      * @param dateUpdated
  486.      */
  487.     @Override
  488.     public void onMapUpdated(MSTMap map, Date dateUpdated) {
  489. //        floorPlanImageUrl = map.getMapImageUrl();
  490. //        Log.d(TAG, floorPlanImageUrl);
  491. //        if (getActivity() != null && (floorPlanImage.getDrawable() == null || this.currentMap == null || !this.currentMap.getMapId().equals(map.getMapId()))) {
  492. //            // Set the current map
  493. //            this.currentMap = map;
  494. //            getActivity().runOnUiThread(new Runnable() {
  495. //                @Override
  496. //                public void run() {
  497. //                    renderImage(floorPlanImageUrl);
  498. //                }
  499. //            });
  500. //        }
  501.     }
  502.  
  503.     /**
  504.      * This method is used for rendering the map image using the url from the MSTMap object received from OnMapUpdated callback
  505.      *
  506.      * @param floorPlanImageUrl map image url
  507.      */
  508.     private void renderImage(final String floorPlanImageUrl) {
  509.         Log.d(TAG, "in picasso");
  510.         addedMap = false;
  511.         Picasso.with(getActivity()).
  512.                 load(floorPlanImageUrl).
  513.                 networkPolicy(NetworkPolicy.OFFLINE).
  514.                 into(floorPlanImage, new Callback() {
  515.                     @Override
  516.                     public void onSuccess() {
  517.                         Log.d(TAG, "Image loaded successfully from the cached");
  518.                         addedMap = true;
  519.                         progressBar.setVisibility(View.GONE);
  520.  
  521.                         if (!scaleFactorCalled) {
  522.                             setupScaleFactorForFloorplan();
  523.                         }
  524.                     }
  525.  
  526.                     @Override
  527.                     public void onError() {
  528.                         Picasso.with(getActivity()).
  529.                                 load(floorPlanImageUrl)
  530.                                 .into(floorPlanImage, new Callback() {
  531.                                     @Override
  532.                                     public void onSuccess() {
  533.                                         progressBar.setVisibility(View.GONE);
  534.                                         addedMap = true;
  535.                                         if (!scaleFactorCalled) {
  536.                                             setupScaleFactorForFloorplan();
  537.                                         }
  538.                                         Log.d(TAG, "Image downloaded from server successfully !!");
  539.                                     }
  540.  
  541.                                     @Override
  542.                                     public void onError() {
  543.                                         progressBar.setVisibility(View.GONE);
  544.                                         Log.d(TAG, "Could not download the image from the server");
  545.                                     }
  546.                                 });
  547.                     }
  548.                 });
  549.     }
  550.  
  551.     //provides the list of vBeacons atatched to your floor plan
  552.     @Override
  553.     public void onVirtualBeaconListUpdated(MSTVirtualBeacon[] virtualBeacons, Date dateUpdated) {
  554.         mstVirtualBeaconMap.clear();
  555.         for (MSTVirtualBeacon vb : virtualBeacons) {
  556.             mstVirtualBeaconMap.put(vb.getVbid(), vb);
  557.         }
  558.     }
  559.  
  560.     //called when notifications are received on entering a zone or passing by a vBecaon
  561.     @Override
  562.     public void onNotificationReceived(Date dateReceived, String message) {
  563.         Log.d(TAG, "notification recieved!!");
  564.         Log.d(TAG, "Message => " + message);
  565.         System.out.println("Message : "+message);
  566.         if (!Utils.isEmptyString(message)) {
  567.             try {
  568.                 JSONObject notificationJSONObject = new JSONObject(message);
  569.                 String type = notificationJSONObject.getString("type");
  570.                 if (type.equalsIgnoreCase("zone-event-vb")) {
  571.                     JSONObject messageObject = notificationJSONObject.optJSONObject("message");
  572.                     if (messageObject != null) {
  573.                         String proximity = messageObject.getString("proximity");
  574.                         if (proximity.equals("near") || proximity.equals("immediate")) {
  575.                             String messageToBeDisplayed = "";
  576.                             String extra = messageObject.getString("Extra");
  577.                             String vbID = messageObject.optString("vbID");
  578.                             if (mstVirtualBeaconMap.containsKey(vbID)) {
  579.                                 MSTVirtualBeacon vb = mstVirtualBeaconMap.get(vbID);
  580.                                 messageToBeDisplayed = vb.getMessage();
  581.                             }
  582.                             if (TextUtils.isEmpty(messageToBeDisplayed)) {
  583.                                 if (TextUtils.isEmpty(extra)) {
  584.                                     messageToBeDisplayed = "You're near the Anonymous VB";
  585.                                 } else {
  586.                                     messageToBeDisplayed = String.format("You're %1$s %2$s", proximity, extra);
  587.                                 }
  588.                             }
  589.                             showNotification(false, messageToBeDisplayed);
  590.                         } else if (proximity.equals("far")) {
  591.                             String messageToBeDisplayed = "";
  592.                             String extra = messageObject.getString("Extra");
  593.                             String vbID = messageObject.optString("vbID");
  594.                             if (mstVirtualBeaconMap.containsKey(vbID)) {
  595.                                 MSTVirtualBeacon vb = mstVirtualBeaconMap.get(vbID);
  596.                                 messageToBeDisplayed = vb.getMessage();
  597.                             }
  598.                             if (TextUtils.isEmpty(messageToBeDisplayed)) {
  599.                                 if (TextUtils.isEmpty(extra)) {
  600.                                     messageToBeDisplayed = "You're far from the Anonymous VB";
  601.                                 } else {
  602.                                     messageToBeDisplayed = String.format("You're %1$s %2$s", proximity, extra);
  603.                                 }
  604.                             }
  605.                             //action can be taken according to the need in case of far beacon
  606.                         }
  607.                     }
  608.  
  609.                 } else if (type.equalsIgnoreCase("zones-events")) {
  610.                     JSONObject messageObject = notificationJSONObject.optJSONObject("message");
  611.                     if (messageObject != null) {
  612.                         String trigger = messageObject.getString("Trigger");
  613.                         if (trigger.equalsIgnoreCase("in")) {
  614.                             String messageToBeDisplayed = "";
  615.                             String extra = messageObject.getString("Extra");
  616.  
  617. //                            Insertkehadirandosen_ble( extra,token);//sini
  618.                             Insertkehadirandosen_ble("B34BE141-7FB5" +
  619.                                     "-457B-B8E9-C2BE4806CACC",token);//sini
  620.  
  621.                             if (TextUtils.isEmpty(extra)) {
  622.                                 messageToBeDisplayed = "You're in the Anonymous Zone";
  623.                             } else {
  624.                                 messageToBeDisplayed = String.format("You're %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("You left the %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