Skip to content

Commit

Permalink
[android] consolidate app and service (#295)
Browse files Browse the repository at this point in the history
The "service" project isn't really necessary given storing and sharing
credentials are already implemented in GMS.

This change allows simpler Fragment management (see it results in
around 300 lines code deletion).
  • Loading branch information
wgtdkp authored Aug 13, 2024
1 parent 639e627 commit 1dac5e3
Show file tree
Hide file tree
Showing 52 changed files with 341 additions and 644 deletions.
8 changes: 4 additions & 4 deletions android/build-commissioner-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ cd ../../../
## Copy shared native libraries
cp "$BUILD_DIR"/src/java/libcommissioner-java.so "$BUILD_DIR"/libs

mkdir -p openthread_commissioner/service/libs
mkdir -p openthread_commissioner/service/src/main/jniLibs/"${ANDROID_ABI}"
cp "$BUILD_DIR"/libs/libotcommissioner.jar openthread_commissioner/service/libs
cp "$BUILD_DIR"/libs/*.so openthread_commissioner/service/src/main/jniLibs/"${ANDROID_ABI}"
mkdir -p openthread_commissioner/app/libs
mkdir -p openthread_commissioner/app/src/main/jniLibs/"${ANDROID_ABI}"
cp "$BUILD_DIR"/libs/libotcommissioner.jar openthread_commissioner/app/libs
cp "$BUILD_DIR"/libs/*.so openthread_commissioner/app/src/main/jniLibs/"${ANDROID_ABI}"
4 changes: 3 additions & 1 deletion android/openthread_commissioner/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
build/
/captures
.externalNativeBuild
.cxx
local.properties
*.jar
*.so
1 change: 0 additions & 1 deletion android/openthread_commissioner/app/.gitignore

This file was deleted.

25 changes: 20 additions & 5 deletions android/openthread_commissioner/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,34 @@ android {
}
}

// This is for the local tests (src/test) to find the native libraries.
tasks.withType(Test) {
systemProperty "java.library.path", "/usr/local/lib"
}

configurations.all {
resolutionStrategy {
force 'androidx.test:core:1.5.0'
}
}

dependencies {
implementation project(':service')
implementation 'org.jetbrains:annotations:15.0'
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

implementation fileTree(dir: "libs", include: ["*.jar"])

implementation 'com.google.guava:guava:31.1-jre'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation "androidx.concurrent:concurrent-futures:1.1.0"
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.navigation:navigation-fragment:2.3.0'
implementation 'com.google.android.gms:play-services-vision:20.1.3+'
implementation 'com.google.android.gms:play-services-threadnetwork:16.0.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'com.google.truth:truth:1.4.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}
11 changes: 10 additions & 1 deletion android/openthread_commissioner/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature
android:name="android.hardware.camera"
android:required="false" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
Expand All @@ -14,7 +24,6 @@
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;
package io.openthread.commissioner.app;

import android.Manifest.permission;
import android.content.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;
package io.openthread.commissioner.app;

import android.os.Parcel;
import android.os.Parcelable;
Expand All @@ -48,9 +48,9 @@ public BorderAgentInfo(
@NonNull byte[] extendedPanId,
@NonNull InetAddress host,
@NonNull int port) {
this.id = id.clone();
this.id = id == null ? null : id.clone();
this.networkName = networkName;
this.extendedPanId = extendedPanId.clone();
this.extendedPanId = extendedPanId == null ? null : extendedPanId.clone();
this.host = host;
this.port = port;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;
package io.openthread.commissioner.app;

import android.Manifest;
import android.content.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;
package io.openthread.commissioner.app;

import androidx.annotation.Nullable;
import androidx.concurrent.futures.CallbackToFutureAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;
package io.openthread.commissioner.app;

import android.app.Activity;
import android.os.Bundle;
Expand All @@ -46,10 +46,10 @@
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;

public class MeshcopFragment extends Fragment
public class CommissioningFragment extends Fragment
implements ThreadCommissionerServiceImpl.IntermediateStateCallback {

private static final String TAG = MeshcopFragment.class.getSimpleName();
private static final String TAG = CommissioningFragment.class.getSimpleName();

TextView statusText;
ProgressBar progressBar;
Expand All @@ -58,24 +58,20 @@ public class MeshcopFragment extends Fragment
ImageView doneImage;
ImageView errorImage;

@NonNull private final FragmentCallback meshcopCallback;
private final FragmentCallback fragmentCallback;
private final ThreadNetworkInfoHolder networkInfoHolder;
private final byte[] pskc;
private final JoinerDeviceInfo joinerDeviceInfo;

@NonNull private final ThreadNetworkInfoHolder networkInfoHolder;

@NonNull private final byte[] pskc;

@NonNull private final JoinerDeviceInfo joinerDeviceInfo;

private final ThreadCommissionerServiceImpl commissionerService =
ThreadCommissionerServiceImpl.newInstance(this);
private ThreadCommissionerServiceImpl commissionerService;
private ListenableFuture<Void> commissionFuture;

public MeshcopFragment(
@NonNull FragmentCallback meshcopCallback,
@NonNull ThreadNetworkInfoHolder networkInfoHolder,
@NonNull byte[] pskc,
@NonNull JoinerDeviceInfo joinerDeviceInfo) {
this.meshcopCallback = meshcopCallback;
public CommissioningFragment(
FragmentCallback fragmentCallback,
ThreadNetworkInfoHolder networkInfoHolder,
byte[] pskc,
JoinerDeviceInfo joinerDeviceInfo) {
this.fragmentCallback = fragmentCallback;
this.networkInfoHolder = networkInfoHolder;
this.pskc = pskc;
this.joinerDeviceInfo = joinerDeviceInfo;
Expand All @@ -91,6 +87,10 @@ public View onCreateView(
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

if (commissionerService == null) {
commissionerService = ThreadCommissionerServiceImpl.newInstance(requireActivity(), this);
}

cancelButton = view.findViewById(R.id.cancel_button);
doneButton = view.findViewById(R.id.done_button);
doneImage = view.findViewById(R.id.done_image);
Expand All @@ -101,13 +101,13 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
view.findViewById(R.id.cancel_button)
.setOnClickListener(
v -> {
meshcopCallback.onMeshcopResult(Activity.RESULT_CANCELED);
fragmentCallback.onAddDeviceResult(Activity.RESULT_CANCELED);
});

view.findViewById(R.id.done_button)
.setOnClickListener(
v -> {
meshcopCallback.onMeshcopResult(Activity.RESULT_OK);
fragmentCallback.onAddDeviceResult(Activity.RESULT_OK);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;
package io.openthread.commissioner.app;

import android.app.AlertDialog;
import android.app.Dialog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;

import androidx.annotation.Nullable;
package io.openthread.commissioner.app;

public interface FragmentCallback {
void onJoinerInfoReceived(@Nullable JoinerDeviceInfo joinerDeviceInfo);

void onNetworkSelected(
@Nullable ThreadNetworkInfoHolder networkInfoHolder, @Nullable byte[] pskc);

void onMeshcopResult(int result);
/**
* Called when adding a new Thread device has finished either successfully or failed.
*
* @param result can be one of RESULT_* of {@link android.app.Activity}
*/
void onAddDeviceResult(int result);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, The OpenThread Commissioner Authors.
* Copyright (c) 2024, The OpenThread Commissioner Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -26,26 +26,34 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

package io.openthread.commissioner.service;
package io.openthread.commissioner.app;

import android.app.Application;
import android.app.Activity;
import android.content.Context;
import androidx.fragment.app.Fragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

/** The Commissioner Service App for getting Application Context from anywhere. */
public class CommissionerServiceApp extends Application {
private static CommissionerServiceApp instance;
public final class FragmentUtils {
private FragmentUtils() {}

public static CommissionerServiceApp getInstance() {
return instance;
/** Shows an alert dialog to the user and exits the commissioning flow when the user consents. */
public static void showAlertAndExit(
Context context, FragmentCallback fragmentCallback, String title, String message) {
new MaterialAlertDialogBuilder(context, R.style.ThreadNetworkAlertTheme)
.setTitle(title)
.setMessage(message)
.setPositiveButton(
"OK", (dialog, which) -> fragmentCallback.onAddDeviceResult(Activity.RESULT_CANCELED))
.show();
}

public static Context getContext() {
return instance.getApplicationContext();
}

@Override
public void onCreate() {
instance = this;
super.onCreate();
/** Shows the next fragment and adds the current fragment to the back stack. */
public static void moveToNextFragment(Fragment currentFragment, Fragment nextFragment) {
currentFragment
.getParentFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, nextFragment, nextFragment.getClass().getSimpleName())
.addToBackStack(/* name= */ null)
.commit();
}
}
Loading

0 comments on commit 1dac5e3

Please sign in to comment.