Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create misc module, add broadcast receiver snippets #397

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/apply_spotless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ jobs:
- name: Run spotlessApply
run: ./gradlew :compose:spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace

- name: Run spotlessApply for Wear
run: ./gradlew :wear:spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace

- name: Run spotlessApply for Misc
run: ./gradlew :misc:spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace

- name: Auto-commit if spotlessApply has changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Apply Spotless

- name: Run spotlessApply for Wear
run: ./gradlew :wear:spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace
commit_message: Apply Spotless
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ jobs:
run: ./gradlew :kotlin:build
- name: Build Wear snippets
run: ./gradlew :wear:build
- name: Build misc snippets
run: ./gradlew :misc:build
1 change: 1 addition & 0 deletions misc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
72 changes: 72 additions & 0 deletions misc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
alias(libs.plugins.compose.compiler)
}

android {
compileSdk = libs.versions.compileSdk.get().toInt()
namespace = "com.example.snippets"

defaultConfig {
applicationId = "com.example.snippets"
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.targetSdk.get().toInt()
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

kotlin {
jvmToolchain(17)
}

buildTypes {
getByName("debug") {
signingConfig = signingConfigs.getByName("debug")
}

getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
buildFeatures {
compose = true
// Disable unused AGP features
viewBinding = true
}

}
dependencies {
val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
androidTestImplementation(composeBom)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.compose.runtime)
implementation(libs.androidx.compose.foundation)
implementation(libs.androidx.compose.foundation.layout)
implementation(libs.androidx.compose.ui.util)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)

implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.kotlinx.serialization.json)
ksp(libs.hilt.compiler)

implementation(libs.androidx.lifecycle.runtime)
testImplementation(libs.junit)
androidTestImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.androidx.test.espresso.core)
}
File renamed without changes.
50 changes: 50 additions & 0 deletions misc/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<permission android:name="com.example.snippets.CUSTOM_PERMISSION"/>

<!--[START android_broadcast_receiver_10_manifest_permission]-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--[END android_broadcast_receiver_10_manifest_permission]-->

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Snippets">

<!-- [START android_broadcast_receiver_1_receiver_manifest]-->
<!-- If this receiver listens for broadcasts sent from the system or from
other apps, even other apps that you own, set android:exported to "true". -->
<receiver android:name=".MyBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.example.snippets.ACTION_UPDATE_DATA" />
</intent-filter>
</receiver>
<!-- [END android_broadcast_receiver_1_receiver_manifest]-->
<!-- [START android_broadcast_receiver_11_receiver_manifest_with_permission]-->
<!-- If this receiver listens for broadcasts sent from the system or from
other apps, even other apps that you own, set android:exported to "true". -->
<receiver
android:name=".MyBroadcastReceiverWithPermission"
android:permission="android.permission.ACCESS_COARSE_LOCATION"
android:exported="true">
<intent-filter>
<action android:name="com.example.snippets.ACTION_UPDATE_DATA" />
</intent-filter>
</receiver>
<!-- [END android_broadcast_receiver_11_receiver_manifest_with_permission]-->
<activity
android:name="com.example.snippets.MainActivity"
android:exported="true"
android:theme="@style/Theme.Snippets">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.example.snippets;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.activity.ComponentActivity;
import androidx.core.content.ContextCompat;

import java.util.Objects;

import javax.inject.Inject;

import dagger.hilt.android.qualifiers.ApplicationContext;

// Warning for reader: This file has the Java code snippets for completeness of the corresponding
// documentation page, but these snippets are not part of the actual sample. Refer to the Kotlin
// code for the actual sample.
public class BroadcastReceiverJavaSnippets {

// [START android_broadcast_receiver_2_class_java]
public static class MyBroadcastReceiver extends BroadcastReceiver {

@Inject
DataRepository dataRepository;

@Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), "com.example.snippets.ACTION_UPDATE_DATA")) {
String data = intent.getStringExtra("com.example.snippets.DATA");
// Do something with the data, for example send it to a data repository:
if (data != null) { dataRepository.updateData(data); }
}
}
}
// [END android_broadcast_receiver_2_class_java]

/** @noinspection ConstantValue, unused */
public static class BroadcastReceiverViewModel {
Context context;

public BroadcastReceiverViewModel(@ApplicationContext Context context) {
this.context = context;
}

// [START android_broadcast_receiver_3_definition_java]
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
// [END android_broadcast_receiver_3_definition_java]

public void registerBroadcastReceiver() {
// [START android_broadcast_receiver_4_intent_filter_java]
IntentFilter filter = new IntentFilter("com.example.snippets.ACTION_UPDATE_DATA");
// [END android_broadcast_receiver_4_intent_filter_java]
// [START android_broadcast_receiver_5_exported_java]
boolean listenToBroadcastsFromOtherApps = false;
int receiverFlags = listenToBroadcastsFromOtherApps
? ContextCompat.RECEIVER_EXPORTED
: ContextCompat.RECEIVER_NOT_EXPORTED;
// [END android_broadcast_receiver_5_exported_java]

// [START android_broadcast_receiver_6_register_java]
ContextCompat.registerReceiver(context, myBroadcastReceiver, filter, receiverFlags);
// [END android_broadcast_receiver_6_register_java]

// [START android_broadcast_receiver_12_register_with_permission_java]
ContextCompat.registerReceiver(
context, myBroadcastReceiver, filter,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
null, // scheduler that defines thread, null means run on main thread
receiverFlags
);
// [END android_broadcast_receiver_12_register_with_permission_java]
}

public void unregisterBroadcastReceiver() {
context.unregisterReceiver(myBroadcastReceiver);
}

public void sendBroadcast(String newData, boolean usePermission) {
if(usePermission) {
// [START android_broadcast_receiver_8_send_java]
Intent intent = new Intent("com.example.snippets.ACTION_UPDATE_DATA");
intent.putExtra("com.example.snippets.DATA", newData);
intent.setPackage("com.example.snippets");
context.sendBroadcast(intent);
// [END android_broadcast_receiver_8_send_java]
} else {
Intent intent = new Intent("com.example.snippets.ACTION_UPDATE_DATA");
intent.putExtra("com.example.snippets.DATA", newData);
intent.setPackage("com.example.snippets");
// [START android_broadcast_receiver_9_send_with_permission_java]
context.sendBroadcast(intent, android.Manifest.permission.ACCESS_COARSE_LOCATION);
// [END android_broadcast_receiver_9_send_with_permission_java]
}
}
}

/** @noinspection InnerClassMayBeStatic*/
// [START android_broadcast_receiver_13_activity_java]
class MyActivity extends ComponentActivity {
MyBroadcastReceiver myBroadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [START_EXCLUDE]
IntentFilter filter = new IntentFilter("com.example.snippets.ACTION_UPDATE_DATA");
boolean listenToBroadcastsFromOtherApps = false;
int receiverFlags = listenToBroadcastsFromOtherApps
? ContextCompat.RECEIVER_EXPORTED
: ContextCompat.RECEIVER_NOT_EXPORTED;
// [END_EXCLUDE]
ContextCompat.registerReceiver(this, myBroadcastReceiver, filter, receiverFlags);
// Set content
}
}
// [END android_broadcast_receiver_13_activity_java]
}
Loading