|
| 1 | +package io.customer.android.sample.java_layout.ui.location; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.pm.PackageManager; |
| 7 | +import android.location.Location; |
| 8 | +import android.location.LocationListener; |
| 9 | +import android.location.LocationManager; |
| 10 | +import android.net.Uri; |
| 11 | +import android.provider.Settings; |
| 12 | +import android.view.MenuItem; |
| 13 | + |
| 14 | +import androidx.activity.result.ActivityResultLauncher; |
| 15 | +import androidx.activity.result.contract.ActivityResultContracts; |
| 16 | +import androidx.annotation.NonNull; |
| 17 | +import androidx.core.content.ContextCompat; |
| 18 | + |
| 19 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
| 20 | +import com.google.android.material.snackbar.Snackbar; |
| 21 | + |
| 22 | +import io.customer.android.sample.java_layout.R; |
| 23 | +import io.customer.android.sample.java_layout.databinding.ActivityLocationTestBinding; |
| 24 | +import io.customer.android.sample.java_layout.ui.core.BaseActivity; |
| 25 | +import io.customer.location.ModuleLocation; |
| 26 | +import io.customer.sdk.CustomerIO; |
| 27 | + |
| 28 | +public class LocationTestActivity extends BaseActivity<ActivityLocationTestBinding> { |
| 29 | + |
| 30 | + private static final double[][] PRESET_COORDS = { |
| 31 | + {40.7128, -74.0060}, // New York |
| 32 | + {51.5074, -0.1278}, // London |
| 33 | + {35.6762, 139.6503}, // Tokyo |
| 34 | + {-33.8688, 151.2093}, // Sydney |
| 35 | + {-23.5505, -46.6333}, // Sao Paulo |
| 36 | + {0.0, 0.0} // 0, 0 |
| 37 | + }; |
| 38 | + private static final String[] PRESET_NAMES = { |
| 39 | + "New York", "London", "Tokyo", "Sydney", "Sao Paulo", "0, 0" |
| 40 | + }; |
| 41 | + |
| 42 | + private LocationManager locationManager; |
| 43 | + private LocationListener locationListener; |
| 44 | + private boolean userRequestedCurrentLocation = false; |
| 45 | + private boolean userRequestedSdkLocation = false; |
| 46 | + |
| 47 | + private final ActivityResultLauncher<String[]> locationPermissionLauncher = |
| 48 | + registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), permissions -> { |
| 49 | + boolean fineGranted = Boolean.TRUE.equals(permissions.get(Manifest.permission.ACCESS_FINE_LOCATION)); |
| 50 | + boolean coarseGranted = Boolean.TRUE.equals(permissions.get(Manifest.permission.ACCESS_COARSE_LOCATION)); |
| 51 | + if (fineGranted || coarseGranted) { |
| 52 | + if (userRequestedCurrentLocation) { |
| 53 | + userRequestedCurrentLocation = false; |
| 54 | + fetchDeviceLocation(); |
| 55 | + } else if (userRequestedSdkLocation) { |
| 56 | + userRequestedSdkLocation = false; |
| 57 | + performSdkLocationRequest(); |
| 58 | + } |
| 59 | + } else { |
| 60 | + showPermissionDeniedAlert(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + @Override |
| 65 | + protected ActivityLocationTestBinding inflateViewBinding() { |
| 66 | + return ActivityLocationTestBinding.inflate(getLayoutInflater()); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void setupContent() { |
| 71 | + if (getSupportActionBar() != null) { |
| 72 | + getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 73 | + getSupportActionBar().setTitle(R.string.location_test_title); |
| 74 | + } |
| 75 | + |
| 76 | + locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
| 77 | + CustomerIO.instance().screen(getString(R.string.location_test_title)); |
| 78 | + |
| 79 | + setupPresetButtons(); |
| 80 | + setupSdkLocationButtons(); |
| 81 | + setupDeviceLocationButton(); |
| 82 | + setupManualEntrySection(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean onOptionsItemSelected(@NonNull MenuItem item) { |
| 87 | + if (item.getItemId() == android.R.id.home) { |
| 88 | + finish(); |
| 89 | + return true; |
| 90 | + } |
| 91 | + return super.onOptionsItemSelected(item); |
| 92 | + } |
| 93 | + |
| 94 | + private void setupPresetButtons() { |
| 95 | + int[] buttonIds = { |
| 96 | + R.id.preset_new_york, R.id.preset_london, R.id.preset_tokyo, |
| 97 | + R.id.preset_sydney, R.id.preset_sao_paulo, R.id.preset_zero |
| 98 | + }; |
| 99 | + for (int i = 0; i < buttonIds.length; i++) { |
| 100 | + final int index = i; |
| 101 | + findViewById(buttonIds[i]).setOnClickListener(v -> |
| 102 | + setLocation(PRESET_COORDS[index][0], PRESET_COORDS[index][1], PRESET_NAMES[index]) |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void setupSdkLocationButtons() { |
| 108 | + binding.requestSdkLocationOnce.setOnClickListener(v -> requestSdkLocationOnce()); |
| 109 | + binding.stopLocationUpdates.setOnClickListener(v -> stopSdkLocationUpdates()); |
| 110 | + } |
| 111 | + |
| 112 | + private void setupDeviceLocationButton() { |
| 113 | + binding.useCurrentLocation.setOnClickListener(v -> requestCurrentLocation()); |
| 114 | + } |
| 115 | + |
| 116 | + private void setupManualEntrySection() { |
| 117 | + binding.setManualLocation.setOnClickListener(v -> setManualLocation()); |
| 118 | + } |
| 119 | + |
| 120 | + // --- Location Actions --- |
| 121 | + |
| 122 | + private void setLocation(double latitude, double longitude, String sourceName) { |
| 123 | + ModuleLocation.instance().getLocationServices().setLastKnownLocation(latitude, longitude); |
| 124 | + String sourceText = sourceName != null ? " (" + sourceName + ")" : ""; |
| 125 | + binding.lastSetLocationLabel.setText( |
| 126 | + getString(R.string.last_set_format, latitude, longitude, sourceText) |
| 127 | + ); |
| 128 | + showSnackbar(getString(R.string.location_set_success, sourceText)); |
| 129 | + } |
| 130 | + |
| 131 | + private void setManualLocation() { |
| 132 | + String latText = binding.latitudeInput.getText() != null |
| 133 | + ? binding.latitudeInput.getText().toString().trim() : ""; |
| 134 | + String lonText = binding.longitudeInput.getText() != null |
| 135 | + ? binding.longitudeInput.getText().toString().trim() : ""; |
| 136 | + |
| 137 | + if (latText.isEmpty() || lonText.isEmpty()) { |
| 138 | + showSnackbar(getString(R.string.enter_valid_coordinates)); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + double latitude = Double.parseDouble(latText); |
| 144 | + double longitude = Double.parseDouble(lonText); |
| 145 | + setLocation(latitude, longitude, "Manual"); |
| 146 | + } catch (NumberFormatException e) { |
| 147 | + showSnackbar(getString(R.string.enter_valid_coordinates)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void requestSdkLocationOnce() { |
| 152 | + if (isLocationPermissionGranted()) { |
| 153 | + performSdkLocationRequest(); |
| 154 | + } else { |
| 155 | + userRequestedSdkLocation = true; |
| 156 | + locationPermissionLauncher.launch(new String[]{ |
| 157 | + Manifest.permission.ACCESS_FINE_LOCATION, |
| 158 | + Manifest.permission.ACCESS_COARSE_LOCATION |
| 159 | + }); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private void performSdkLocationRequest() { |
| 164 | + binding.lastSetLocationLabel.setText(R.string.requesting_location_sdk); |
| 165 | + ModuleLocation.instance().getLocationServices().requestLocationUpdate(); |
| 166 | + showSnackbar(getString(R.string.sdk_requested_location)); |
| 167 | + } |
| 168 | + |
| 169 | + private void stopSdkLocationUpdates() { |
| 170 | + ModuleLocation.instance().getLocationServices().stopLocationUpdates(); |
| 171 | + binding.lastSetLocationLabel.setText(R.string.location_updates_stopped); |
| 172 | + showSnackbar(getString(R.string.stopped_location_updates)); |
| 173 | + } |
| 174 | + |
| 175 | + private void requestCurrentLocation() { |
| 176 | + if (isLocationPermissionGranted()) { |
| 177 | + fetchDeviceLocation(); |
| 178 | + } else { |
| 179 | + userRequestedCurrentLocation = true; |
| 180 | + locationPermissionLauncher.launch(new String[]{ |
| 181 | + Manifest.permission.ACCESS_FINE_LOCATION, |
| 182 | + Manifest.permission.ACCESS_COARSE_LOCATION |
| 183 | + }); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @SuppressWarnings("MissingPermission") |
| 188 | + private void fetchDeviceLocation() { |
| 189 | + binding.useCurrentLocation.setEnabled(false); |
| 190 | + binding.useCurrentLocation.setText(R.string.fetching_location); |
| 191 | + |
| 192 | + locationListener = new LocationListener() { |
| 193 | + @Override |
| 194 | + public void onLocationChanged(@NonNull Location location) { |
| 195 | + locationManager.removeUpdates(this); |
| 196 | + setLocation(location.getLatitude(), location.getLongitude(), "Device"); |
| 197 | + binding.useCurrentLocation.setEnabled(true); |
| 198 | + binding.useCurrentLocation.setText(R.string.use_current_location); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void onProviderDisabled(@NonNull String provider) { |
| 203 | + showSnackbar(getString(R.string.location_not_available)); |
| 204 | + binding.useCurrentLocation.setEnabled(true); |
| 205 | + binding.useCurrentLocation.setText(R.string.use_current_location); |
| 206 | + } |
| 207 | + }; |
| 208 | + |
| 209 | + String provider = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) |
| 210 | + ? LocationManager.GPS_PROVIDER |
| 211 | + : LocationManager.NETWORK_PROVIDER; |
| 212 | + |
| 213 | + locationManager.requestSingleUpdate(provider, locationListener, null); |
| 214 | + } |
| 215 | + |
| 216 | + // --- Permission Helpers --- |
| 217 | + |
| 218 | + private boolean isLocationPermissionGranted() { |
| 219 | + return ContextCompat.checkSelfPermission(this, |
| 220 | + Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED |
| 221 | + || ContextCompat.checkSelfPermission(this, |
| 222 | + Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED; |
| 223 | + } |
| 224 | + |
| 225 | + private void showPermissionDeniedAlert() { |
| 226 | + new MaterialAlertDialogBuilder(this) |
| 227 | + .setTitle(R.string.location_permission_required) |
| 228 | + .setMessage(R.string.location_permission_failure) |
| 229 | + .setNeutralButton(R.string.open_settings, (dialog, which) -> { |
| 230 | + Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 231 | + intent.setData(Uri.parse("package:" + getPackageName())); |
| 232 | + startActivity(intent); |
| 233 | + }) |
| 234 | + .show(); |
| 235 | + } |
| 236 | + |
| 237 | + private void showSnackbar(String message) { |
| 238 | + Snackbar.make(binding.getRoot(), message, Snackbar.LENGTH_SHORT).show(); |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + protected void onDestroy() { |
| 243 | + super.onDestroy(); |
| 244 | + if (locationListener != null && locationManager != null) { |
| 245 | + locationManager.removeUpdates(locationListener); |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments