document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.ngulikode.arahkiblat;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.core.app.ActivityCompat;
  6.  
  7. import android.Manifest;
  8. import android.annotation.SuppressLint;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.content.pm.PackageManager;
  12. import android.os.Build;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.view.animation.Animation;
  17. import android.view.animation.RotateAnimation;
  18. import android.widget.ImageView;
  19. import android.widget.RelativeLayout;
  20. import android.widget.Toast;
  21.  
  22. import static android.view.View.INVISIBLE;
  23.  
  24. public class MainActivity extends AppCompatActivity {
  25.     private static final String TAG = "CompasActivity";
  26.     private Compass compass;
  27.     private RelativeLayout arrowViewQiblat;
  28.     private ImageView imageDial;
  29.     private float currentAzimuth;
  30.     SharedPreferences prefs;
  31.     GPSTracker gps;
  32.  
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_main);
  37.  
  38.         prefs = getSharedPreferences("", MODE_PRIVATE);
  39.         arrowViewQiblat = (RelativeLayout) findViewById(R.id.main_image_qiblat);
  40.         gps = new GPSTracker(this);
  41.         imageDial = (ImageView) findViewById(R.id.main_image_dial);
  42.  
  43.         arrowViewQiblat.setVisibility(INVISIBLE);
  44.         arrowViewQiblat.setVisibility(View.GONE);
  45.  
  46.         setupCompass();
  47.         fetch_GPS();
  48.     }
  49.  
  50.     @Override
  51.     protected void onStart() {
  52.         super.onStart();
  53. //        setActivityAvailable(true);
  54.         Log.d(TAG, "start compas");
  55.         if(compass != null){
  56.             compass.start();
  57.         }
  58.     }
  59.  
  60.     @Override
  61.     protected void onStop() {
  62.         super.onStop();
  63.         Log.d(TAG, "stop compass");
  64.         if(compass != null){
  65.             compass.stop();
  66.         }
  67.     }
  68.  
  69.     @Override
  70.     protected void onPause() {
  71.         super.onPause();
  72.         if(compass != null){
  73.             compass.stop();
  74.         }
  75.     }
  76.  
  77.     @Override
  78.     protected void onResume() {
  79.         super.onResume();
  80.         if(compass != null){
  81.             compass.start();
  82.         }
  83.     }
  84.  
  85.     private void setupCompass() {
  86.         Boolean permission_granted = GetBoolean("permission_granted");
  87.         if(permission_granted) {
  88.             getBearing();
  89.         }else{
  90.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  91.                 ActivityCompat.requestPermissions(this,
  92.                         new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
  93.                         1);
  94.             }
  95.         }
  96.  
  97.         compass = new Compass(this);
  98.         Compass.CompassListener cl = new Compass.CompassListener() {
  99.  
  100.             @Override
  101.             public void onNewAzimuth(float azimuth) {
  102.                 // adjustArrow(azimuth);
  103.                 adjustGambarDial(azimuth);
  104.                 adjustArrowQiblat(azimuth);
  105.             }
  106.         };
  107.         compass.setListener(cl);
  108.     }
  109.  
  110.     public void adjustGambarDial(float azimuth) {
  111.          Log.d(TAG, "will set rotation from " + currentAzimuth + " to "                + azimuth);
  112.  
  113.         Animation an = new RotateAnimation(-currentAzimuth, -azimuth,
  114.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
  115.                 0.5f);
  116.         currentAzimuth = (azimuth);
  117.         an.setDuration(500);
  118.         an.setRepeatCount(0);
  119.         an.setFillAfter(true);
  120.         imageDial.startAnimation(an);
  121.     }
  122.     public void adjustArrowQiblat(float azimuth) {
  123.         Log.d(TAG, "will set rotation from " + currentAzimuth + " to "                + azimuth);
  124.  
  125.         float kiblat_derajat = GetFloat("kiblat_derajat");
  126.         Animation an = new RotateAnimation(-(currentAzimuth)+kiblat_derajat, -azimuth,
  127.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
  128.                 0.5f);
  129.         currentAzimuth = (azimuth);
  130.         an.setDuration(500);
  131.         an.setRepeatCount(0);
  132.         an.setFillAfter(true);
  133.         arrowViewQiblat.startAnimation(an);
  134.         if(kiblat_derajat > 0){
  135.             arrowViewQiblat.setVisibility(View.VISIBLE);
  136.         }else{
  137.             arrowViewQiblat.setVisibility(INVISIBLE);
  138.             arrowViewQiblat.setVisibility(View.GONE);
  139.         }
  140.     }
  141.  
  142.     @SuppressLint("MissingPermission")
  143.     public void getBearing(){
  144.         // Get the location manager
  145.         float kiblat_derajat = GetFloat("kiblat_derajat");
  146.         if(kiblat_derajat > 0.0001){
  147.             arrowViewQiblat .setVisibility(View.VISIBLE);
  148.         }else
  149.         {
  150.             fetch_GPS();
  151.         }
  152.     }
  153.  
  154.     @Override
  155.     public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResult) {
  156.         switch (requestCode) {
  157.             case 1: {
  158.                 if (grantResult.length > 0 && grantResult[0] == PackageManager.PERMISSION_GRANTED) {
  159.                     SaveBoolean("permission_granted", true);
  160. //                    onStart();
  161.                     arrowViewQiblat.setVisibility(INVISIBLE);
  162.                     arrowViewQiblat.setVisibility(View.GONE);
  163.                     setupCompass();
  164.                 } else {
  165.                     Toast.makeText(getApplicationContext(), "Permision not allowed!", Toast.LENGTH_LONG).show();
  166.                     finish();
  167.                 }
  168.                 return;
  169.             }
  170.         }
  171.         super.onRequestPermissionsResult(requestCode, permissions, grantResult);
  172.     }
  173.  
  174.     public void SaveBoolean(String Judul, Boolean bbb){
  175.         SharedPreferences.Editor edit = prefs.edit();
  176.         edit.putBoolean(Judul, bbb);
  177.         edit.apply();
  178.     }
  179.     public Boolean GetBoolean(String Judul){
  180.         Boolean result = prefs.getBoolean(Judul, false);
  181.         return result;
  182.     }
  183.  
  184.     public void SaveFloat(String Judul, Float bbb){
  185.         SharedPreferences.Editor edit = prefs.edit();
  186.         edit.putFloat(Judul, bbb);
  187.         edit.apply();
  188.     }
  189.  
  190.     public Float GetFloat(String Judul){
  191.         Float xxxxxx = prefs.getFloat(Judul, 0);
  192.         return xxxxxx;
  193.     }
  194.  
  195.     public void fetch_GPS(){
  196.         double result = 0;
  197.         gps = new GPSTracker(this);
  198.         if(gps.canGetLocation()){
  199.             // \\n is for new line
  200.             Log.e("TAG", "GPS is on");
  201.             double lat_saya = gps.getLatitude();
  202.             double lon_saya = gps.getLongitude();
  203.             if(lat_saya < 0.001 && lon_saya < 0.001) {
  204.                 // arrowViewQiblat.isShown(false);
  205.                 arrowViewQiblat.setVisibility(INVISIBLE);
  206.                 arrowViewQiblat.setVisibility(View.GONE);
  207.                 // Toast.makeText(getApplicationContext(), "Location not ready, Please Restart Application", Toast.LENGTH_LONG).show();
  208.             }else{
  209.                 double longitude2 = 39.826206; // ka\'bah Position https://www.latlong.net/place/kaaba-mecca-saudi-arabia-12639.html
  210.                 double longitude1 = lon_saya;
  211.                 double latitude2 = Math.toRadians(21.422487); // ka\'bah Position https://www.latlong.net/place/kaaba-mecca-saudi-arabia-12639.html
  212.                 double latitude1 = Math.toRadians(lat_saya);
  213.                 double longDiff= Math.toRadians(longitude2-longitude1);
  214.                 double y= Math.sin(longDiff)*Math.cos(latitude2);
  215.                 double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);
  216.                 result = (Math.toDegrees(Math.atan2(y, x))+360)%360;
  217.                 float result2 = (float)result;
  218.                 SaveFloat("kiblat_derajat", result2);
  219.                 arrowViewQiblat.setVisibility(View.VISIBLE);
  220.             }
  221.             //  Toast.makeText(getApplicationContext(), "lat_saya: "+lat_saya + "\\nlon_saya: "+lon_saya, Toast.LENGTH_LONG).show();
  222.         }else{
  223.             // can\'t get location
  224.             // GPS or Network is not enabled
  225.             // Ask user to enable GPS/network in settings
  226.             gps.showSettingsAlert();
  227.  
  228.             // arrowViewQiblat.isShown(false);
  229.             arrowViewQiblat .setVisibility(INVISIBLE);
  230.             arrowViewQiblat .setVisibility(View.GONE);
  231.             // Toast.makeText(getApplicationContext(), "Please enable Location first and Restart Application", Toast.LENGTH_LONG).show();
  232.         }
  233.     }
  234. }
');