Advertisement
tim_rodaway

AR.java

Apr 9th, 2018
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. package uk.ac.ncl.b5027438.CEG3799;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.hardware.SensorManager;
  6. import android.location.LocationProvider;
  7. import android.support.v4.app.ActivityCompat;
  8. import android.support.v4.content.ContextCompat;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.webkit.WebView;
  12. import android.location.Location;
  13. import android.location.LocationListener;
  14. import android.widget.Toast;
  15.  
  16. import com.google.android.gms.location.FusedLocationProviderClient;
  17. import com.google.android.gms.location.LocationServices;
  18. import com.google.android.gms.tasks.OnSuccessListener;
  19. import com.wikitude.architect.ArchitectStartupConfiguration;
  20. import com.wikitude.architect.ArchitectView;
  21. import com.wikitude.tools.location.LocationService;
  22.  
  23.  
  24. public class AR extends AppCompatActivity implements LocationListener {
  25.     public static final String INTENT_EXTRAS_KEY_SAMPLE = "sampleData";
  26.  
  27.     private static final String TAG = SimpleArActivity.class.getSimpleName();
  28.  
  29.     /** Root directory of the sample AR-Experiences in the assets dir. */
  30.     private static final String SAMPLES_ROOT = "samples/";
  31.  
  32.     protected ArchitectView architectView;
  33.  
  34.     /** The path to the AR-Experience. This is usually the path to its index.html. */
  35.     private String arExperience = "AR.html";
  36.  
  37.     private LocationProvider locationProvider;
  38.  
  39.     /** private WebView mWebView; */
  40.  
  41.     private FusedLocationProviderClient mFusedLocationClient;
  42.  
  43.     private void startLocationUpdates() {
  44.         if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  45.             ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, },
  46.                     LocationService.MY_PERMISSION_ACCESS_COARSE_LOCATION);
  47.             ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  48.                     LocationService.MY_PERMISSION_ACCESS_FINE_LOCATION);
  49.         }
  50.         mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  51.     }
  52.  
  53.     private final LocationProvider.ErrorCallback errorCallback = new LocationProvider.ErrorCallback() {
  54.         @Override
  55.         public void noProvidersEnabled() {
  56.             Toast.makeText(AR.this, R.string.no_location_provider, Toast.LENGTH_LONG).show();
  57.         }
  58.     };
  59.  
  60.     private final ArchitectView.SensorAccuracyChangeListener sensorAccuracyChangeListener = new ArchitectView.SensorAccuracyChangeListener() {
  61.         @Override
  62.         public void onCompassAccuracyChanged(int accuracy) {
  63.             if ( accuracy < SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) { // UNRELIABLE = 0, LOW = 1, MEDIUM = 2, HIGH = 3
  64.                 Toast.makeText(AR.this, R.string.compass_accuracy_low, Toast.LENGTH_LONG ).show();
  65.             }
  66.         }
  67.     };
  68.  
  69.     @Override
  70.     protected void onResume() {
  71.         super.onResume();
  72.         if (mRequestingLocationUpdates) {
  73.             startLocationUpdates();
  74.         }
  75.         locationProvider.onResume();
  76.         architectView.registerSensorAccuracyChangeListener(sensorAccuracyChangeListener);
  77.     }
  78.  
  79.     @Override
  80.     protected void onPause() {
  81.         locationProvider.onPause();
  82.         super.onPause();
  83.         architectView.unregisterSensorAccuracyChangeListener(sensorAccuracyChangeListener);
  84.     }
  85.  
  86.     @Override
  87.     public void onLocationChanged(Location location) {
  88.         float accuracy = location.hasAccuracy() ? location.getAccuracy() : 1000;
  89.         if (location.hasAltitude()) {
  90.             architectView.setLocation(location.getLatitude(), location.getLongitude(), location.getAltitude(), accuracy);
  91.         } else {
  92.             architectView.setLocation(location.getLatitude(), location.getLongitude(), accuracy);
  93.         }
  94.     }
  95.  
  96.     @Override
  97.     public void onStatusChanged(String provider, int status, Bundle extras) {}
  98.  
  99.     @Override
  100.     public void onProviderEnabled(String provider) {}
  101.  
  102.     @Override
  103.     public void onProviderDisabled(String provider) {}
  104.  
  105.     protected void onCreate(ArchitectStartupConfiguration) {}
  106.  
  107.     @Override
  108.     protected void onCreate(Bundle savedInstanceState) {
  109.         mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  110.  
  111.         super.onCreate(savedInstanceState);
  112.  
  113.         // Used to enabled remote debugging of the ArExperience with google chrome https://developers.google.com/web/tools/chrome-devtools/remote-debugging
  114.         WebView.setWebContentsDebuggingEnabled(true);
  115.  
  116.         locationProvider = new LocationProvider(this, this, errorCallback);
  117.  
  118.         setContentView(R.layout.activity_ar);
  119.  
  120.         this.architectView = (ArchitectView)this.findViewById( R.id.architectView );
  121.  
  122.         final ArchitectStartupConfiguration config = new ArchitectStartupConfiguration();
  123.         config.setLicenseKey(getString(R.string.wikitude_license_key));
  124.  
  125.         architectView = new ArchitectView(this);
  126.         architectView.onCreate(config); // create ArchitectView with configuration
  127.  
  128.         setContentView(architectView);
  129.  
  130.         this.architectView.onPostCreate();
  131.  
  132.         this.architectView.load( "assets/AR.html" );
  133.  
  134.         /** mWebView = (WebView) findViewById(R.id.architectView); */
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement