Guest User

Untitled

a guest
May 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. public class LocationActivity extends AppCompatActivity
  2. implements LocationContract.View {
  3.  
  4. private static final int REQ_CODE = 111;
  5.  
  6. @BindView(R.id.latitudeTextView)
  7. TextView latitudeTextView;
  8. @BindView(R.id.longitudeTextView)
  9. TextView longitudeTextView;
  10. @BindView(R.id.softDenyTextView)
  11. TextView softDeniedWarningTextView;
  12. @BindView(R.id.hardDenyTextView)
  13. TextView hardDeniedWarningTextView;
  14. @BindViews({R.id.softDenyTextView, R.id.hardDenyTextView})
  15. List<TextView> deniedTextViews;
  16.  
  17. private static ButterKnife.Action<View> VISIBLE =
  18. (v, index) -> v.setVisibility(View.VISIBLE);
  19. private static ButterKnife.Action<View> GONE =
  20. (v, index) -> v.setVisibility(View.GONE);
  21.  
  22. @Inject
  23. LocationContract.Presenter presenter;
  24.  
  25. private FusedLocationProviderClient fusedLocationProviderClient;
  26.  
  27. public static Intent newIntent(Context context) {
  28. return new Intent(context, LocationActivity.class);
  29. }
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. AndroidInjection.inject(this);
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.location_activity);
  36.  
  37. ButterKnife.bind(this);
  38.  
  39. fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
  40. }
  41.  
  42. @Override
  43. protected void onStart() {
  44. super.onStart();
  45. if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  46. == PackageManager.PERMISSION_GRANTED) {
  47. hidePermissionDeniedWarning();
  48. getLocation();
  49. } else {
  50. requestPermission();
  51. }
  52. }
  53.  
  54. @Override
  55. public void showLatitude(String latitude) {
  56. latitudeTextView.setText(latitude);
  57. }
  58.  
  59. @Override
  60. public void showLongitude(String longitude) {
  61. longitudeTextView.setText(longitude);
  62. }
  63.  
  64. @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
  65. @SuppressWarnings({"MissingPermission"})
  66. private void getLocation() {
  67. fusedLocationProviderClient.getLastLocation()
  68. .addOnSuccessListener(location -> {
  69. if (location != null) {
  70. presenter.onLocationAvailable(location.getLatitude(),
  71. location.getLongitude());
  72. } else {
  73. Toast.makeText(LocationActivity.this,
  74. R.string.error_accessing_location,
  75. Toast.LENGTH_SHORT)
  76. .show();
  77. Log.w("LOCATION", "Are you using an emulator? " +
  78. "Make sure you send a dummy location to the emulator " +
  79. "through the emulator settings");
  80. }
  81. });
  82. }
  83.  
  84. private void requestPermission() {
  85. ActivityCompat.requestPermissions(this,
  86. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  87. REQ_CODE);
  88. }
  89.  
  90. @Override
  91. @SuppressWarnings({"MissingPermission"})
  92. public void onRequestPermissionsResult(int requestCode,
  93. @NonNull String[] permissions,
  94. @NonNull int[] grantResults) {
  95. if (requestCode == REQ_CODE) {
  96. if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  97. hidePermissionDeniedWarning();
  98. getLocation();
  99. } else {
  100. handlePermissionDenied();
  101. }
  102. } else {
  103. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  104. }
  105. }
  106.  
  107. private void handlePermissionDenied() {
  108. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  109. Manifest.permission.ACCESS_FINE_LOCATION)) {
  110. showSoftPermissionDeniedWarning();
  111. } else {
  112. // user has checked the "Do not ask again" checkbox
  113. showHardPermissionDeniedWarning();
  114. }
  115. }
  116.  
  117. private void hidePermissionDeniedWarning() {
  118. ButterKnife.apply(deniedTextViews, GONE);
  119. }
  120.  
  121. private void showSoftPermissionDeniedWarning() {
  122. ButterKnife.apply(softDeniedWarningTextView, VISIBLE);
  123. ButterKnife.apply(hardDeniedWarningTextView, GONE);
  124. }
  125.  
  126. private void showHardPermissionDeniedWarning() {
  127. ButterKnife.apply(hardDeniedWarningTextView, VISIBLE);
  128. ButterKnife.apply(softDeniedWarningTextView, GONE);
  129. }
  130.  
  131. @OnClick(R.id.softDenyTextView)
  132. void softDenyTextViewClicked(View view) {
  133. requestPermission();
  134. }
  135.  
  136. @OnClick(R.id.hardDenyTextView)
  137. void hardDenyTextViewClicked(View view) {
  138. Intent intent = new Intent();
  139. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  140. Uri uri = Uri.fromParts("package", getPackageName(), null);
  141. intent.setData(uri);
  142. startActivity(intent);
  143. }
  144.  
  145. }
Add Comment
Please, Sign In to add comment