Skip to content

Commit bde23f5

Browse files
Shahroz16claude
andcommitted
feat: integrate location module into sample apps
Register ModuleLocation in both sample apps. Add location testing UI to the Java sample app: request location permission, set manual location, and request one-shot SDK-managed location. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e8f0aae commit bde23f5

File tree

6 files changed

+114
-1
lines changed

6 files changed

+114
-1
lines changed

samples/java_layout/src/main/java/io/customer/android/sample/java_layout/sdk/CustomerIORepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.customer.android.sample.java_layout.support.Optional;
1414
import io.customer.messaginginapp.MessagingInAppModuleConfig;
1515
import io.customer.messaginginapp.ModuleMessagingInApp;
16+
import io.customer.location.ModuleLocation;
1617
import io.customer.messagingpush.ModuleMessagingPushFCM;
1718
import io.customer.sdk.CustomerIO;
1819
import io.customer.sdk.CustomerIOConfig;
@@ -38,6 +39,9 @@ public void initializeSdk(SampleApplication application) {
3839
// Enables push notification
3940
builder.addCustomerIOModule(new ModuleMessagingPushFCM());
4041

42+
// Enables location tracking
43+
builder.addCustomerIOModule(new ModuleLocation());
44+
4145
// Enables in-app messages
4246
if (sdkConfig.isInAppMessagingEnabled()) {
4347
builder.addCustomerIOModule(new ModuleMessagingInApp(

samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ui/dashboard/DashboardActivity.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.customer.android.sample.java_layout.ui.user.AuthViewModel;
4141
import io.customer.android.sample.java_layout.utils.Randoms;
4242
import io.customer.android.sample.java_layout.utils.ViewUtils;
43+
import io.customer.location.ModuleLocation;
4344
import io.customer.sdk.CustomerIO;
4445

4546
public class DashboardActivity extends BaseActivity<ActivityDashboardBinding> {
@@ -63,6 +64,17 @@ public class DashboardActivity extends BaseActivity<ActivityDashboardBinding> {
6364
}
6465
});
6566

67+
private final ActivityResultLauncher<String[]> locationPermissionRequestLauncher =
68+
registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), permissions -> {
69+
boolean fineGranted = Boolean.TRUE.equals(permissions.get(Manifest.permission.ACCESS_FINE_LOCATION));
70+
boolean coarseGranted = Boolean.TRUE.equals(permissions.get(Manifest.permission.ACCESS_COARSE_LOCATION));
71+
if (fineGranted || coarseGranted) {
72+
showSnackbar(R.string.location_permission_success);
73+
} else {
74+
showLocationPermissionDeniedAlert();
75+
}
76+
});
77+
6678
@Override
6779
protected ActivityDashboardBinding inflateViewBinding() {
6880
return ActivityDashboardBinding.inflate(getLayoutInflater());
@@ -149,6 +161,17 @@ private void setupViews() {
149161
binding.showPushPromptButton.setOnClickListener(view -> {
150162
requestNotificationPermission();
151163
});
164+
binding.requestLocationPermissionButton.setOnClickListener(view -> {
165+
requestLocationPermission();
166+
});
167+
binding.setManualLocationButton.setOnClickListener(view -> {
168+
ModuleLocation.instance().getLocationServices().setLastKnownLocation(37.7749, -122.4194);
169+
showSnackbar(R.string.location_sent_successfully);
170+
});
171+
binding.requestLocationOnceButton.setOnClickListener(view -> {
172+
ModuleLocation.instance().getLocationServices().requestLocationUpdate();
173+
showSnackbar(R.string.location_request_sent);
174+
});
152175
binding.inlineExamplesButton.setOnClickListener(view -> {
153176
startInlineExamplesActivity();
154177
});
@@ -262,6 +285,39 @@ private void showPushPermissionDeniedAlert(@StringRes int messageResId) {
262285
builder.show();
263286
}
264287

288+
private void requestLocationPermission() {
289+
if (isLocationPermissionGranted()) {
290+
showSnackbar(R.string.location_permission_already_granted);
291+
} else {
292+
locationPermissionRequestLauncher.launch(new String[]{
293+
Manifest.permission.ACCESS_FINE_LOCATION,
294+
Manifest.permission.ACCESS_COARSE_LOCATION
295+
});
296+
}
297+
}
298+
299+
private boolean isLocationPermissionGranted() {
300+
return ContextCompat.checkSelfPermission(this,
301+
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
302+
|| ContextCompat.checkSelfPermission(this,
303+
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
304+
}
305+
306+
private void showLocationPermissionDeniedAlert() {
307+
MaterialAlertDialogBuilder builder = ViewUtils.createAlertDialog(this);
308+
builder.setMessage(R.string.location_permission_failure);
309+
builder.setNeutralButton(R.string.open_settings, (dialogInterface, i) -> {
310+
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
311+
intent.setData(Uri.parse("package:" + getPackageName()));
312+
startActivity(intent);
313+
});
314+
builder.show();
315+
}
316+
317+
private void showSnackbar(@StringRes int messageResId) {
318+
Snackbar.make(binding.getRoot(), messageResId, Snackbar.LENGTH_SHORT).show();
319+
}
320+
265321
private void copyToClipboard(String text) {
266322
if (text.isEmpty()) {
267323
return;

samples/java_layout/src/main/res/layout/activity_dashboard.xml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,51 @@
179179
android:layout_height="wrap_content"
180180
android:layout_marginTop="@dimen/margin_default"
181181
android:text="@string/show_push_prompt"
182-
app:layout_constraintBottom_toTopOf="@id/view_logs_button"
182+
app:layout_constraintBottom_toTopOf="@id/request_location_permission_button"
183183
app:layout_constraintEnd_toEndOf="parent"
184184
app:layout_constraintStart_toStartOf="parent"
185185
app:layout_constraintTop_toBottomOf="@id/set_profile_attributes_button"
186186
app:layout_constraintWidth_max="@dimen/material_button_max_width" />
187187

188+
<Button
189+
android:id="@+id/request_location_permission_button"
190+
style="?attr/materialButtonStyle"
191+
android:layout_width="0dp"
192+
android:layout_height="wrap_content"
193+
android:layout_marginTop="@dimen/margin_default"
194+
android:text="@string/request_location_permission"
195+
app:layout_constraintBottom_toTopOf="@id/set_manual_location_button"
196+
app:layout_constraintEnd_toEndOf="parent"
197+
app:layout_constraintStart_toStartOf="parent"
198+
app:layout_constraintTop_toBottomOf="@id/show_push_prompt_button"
199+
app:layout_constraintWidth_max="@dimen/material_button_max_width" />
200+
201+
<Button
202+
android:id="@+id/set_manual_location_button"
203+
style="?attr/materialButtonStyle"
204+
android:layout_width="0dp"
205+
android:layout_height="wrap_content"
206+
android:layout_marginTop="@dimen/margin_default"
207+
android:text="@string/set_manual_location"
208+
app:layout_constraintBottom_toTopOf="@id/request_location_once_button"
209+
app:layout_constraintEnd_toEndOf="parent"
210+
app:layout_constraintStart_toStartOf="parent"
211+
app:layout_constraintTop_toBottomOf="@id/request_location_permission_button"
212+
app:layout_constraintWidth_max="@dimen/material_button_max_width" />
213+
214+
<Button
215+
android:id="@+id/request_location_once_button"
216+
style="?attr/materialButtonStyle"
217+
android:layout_width="0dp"
218+
android:layout_height="wrap_content"
219+
android:layout_marginTop="@dimen/margin_default"
220+
android:text="@string/request_location_once"
221+
app:layout_constraintBottom_toTopOf="@id/view_logs_button"
222+
app:layout_constraintEnd_toEndOf="parent"
223+
app:layout_constraintStart_toStartOf="parent"
224+
app:layout_constraintTop_toBottomOf="@id/set_manual_location_button"
225+
app:layout_constraintWidth_max="@dimen/material_button_max_width" />
226+
188227
<Button
189228
android:id="@+id/view_logs_button"
190229
style="?attr/materialButtonStyle"

samples/java_layout/src/main/res/values/strings.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@
9393
<string name="title_inline_recycler">RecyclerView</string>
9494
<string name="inline_examples_tabbed">Inline Examples</string>
9595

96+
<!-- Location -->
97+
<string name="request_location_permission">Request Location Permission</string>
98+
<string name="set_manual_location">Set Manual Location</string>
99+
<string name="request_location_once">Request Location Once</string>
100+
<string name="location_permission_success">Location permission granted</string>
101+
<string name="location_permission_failure">Location permission denied. Please allow location permission from settings.</string>
102+
<string name="location_permission_already_granted">Location permission already granted</string>
103+
<string name="location_sent_successfully">Manual location sent successfully</string>
104+
<string name="location_request_sent">Location request sent</string>
105+
96106
</resources>

samples/kotlin_compose/src/main/java/io/customer/android/sample/kotlin_compose/MainApplication.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.app.Application
44
import io.customer.android.sample.kotlin_compose.data.models.setValuesFromBuilder
55
import io.customer.android.sample.kotlin_compose.data.sdk.InAppMessageEventListener
66
import io.customer.android.sample.kotlin_compose.di.ServiceLocator
7+
import io.customer.location.ModuleLocation
78
import io.customer.messaginginapp.MessagingInAppModuleConfig
89
import io.customer.messaginginapp.ModuleMessagingInApp
910
import io.customer.messagingpush.ModuleMessagingPushFCM
@@ -37,6 +38,7 @@ class MainApplication : Application() {
3738
)
3839
)
3940
.addCustomerIOModule(ModuleMessagingPushFCM())
41+
.addCustomerIOModule(ModuleLocation())
4042
configuration.setValuesFromBuilder(builder)
4143

4244
CustomerIO.initialize(builder.build())

samples/sample-app.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ dependencies {
128128
implementation(project(":messaginginapp"))
129129
// messaginginapp-compose adds messaginginapp transitively
130130
implementation(project(":messaginginapp-compose"))
131+
implementation(project(":location"))
131132
} else {
132133
// Stable releases dependency, use published versions directly
133134
implementation "io.customer.android:datapipelines:$cioSDKVersion"
134135
implementation "io.customer.android:messaging-push-fcm:$cioSDKVersion"
135136
implementation "io.customer.android:messaging-in-app:$cioSDKVersion"
136137
// messaginginapp-compose adds messaginginapp transitively
137138
implementation "io.customer.android:messaging-in-app-compose:$cioSDKVersion"
139+
implementation "io.customer.android:location:$cioSDKVersion"
138140
}
139141

140142
// Add Compose dependencies for all sample projects

0 commit comments

Comments
 (0)