From a786665dd448d653870bdd93f45ca3eaf4617992 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Wed, 22 Feb 2023 13:59:44 -0800 Subject: [PATCH] [Android] Convert the last java file in Android CHIPTool to kotlin (#25236) * Convert NetworkCredentialsParcelable.java to NetworkCredentialsParcelable.kt * Address review comments --- .../NetworkCredentialsParcelable.java | 170 ------------------ .../chiptool/NetworkCredentialsParcelable.kt | 149 +++++++++++++++ 2 files changed, 149 insertions(+), 170 deletions(-) delete mode 100644 examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.java create mode 100644 examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.kt diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.java b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.java deleted file mode 100644 index 2194155f4f660b..00000000000000 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (c) 2020-2022 Project CHIP Authors - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package com.google.chip.chiptool; - -import android.os.Parcel; -import android.os.Parcelable; -import androidx.annotation.Nullable; - -/** Class for holding WiFi or Thread credentials, but not both. */ -public class NetworkCredentialsParcelable implements Parcelable { - @Nullable private WiFiCredentials wifiCredentials; - @Nullable private ThreadCredentials threadCredentials; - - private NetworkCredentialsParcelable( - @Nullable WiFiCredentials wifiCredentials, @Nullable ThreadCredentials threadCredentials) { - this.wifiCredentials = wifiCredentials; - this.threadCredentials = threadCredentials; - } - - /** - * Return a NetworkCredentialsParcelable object with the given WiFiCredentials and null - * ThreadCredentials. - */ - public static NetworkCredentialsParcelable forWiFi(WiFiCredentials wifiCredentials) { - return new NetworkCredentialsParcelable(wifiCredentials, null); - } - - /** - * Return a NetworkCredentialsParcelable object with the given ThreadCredentials and null - * WiFiCredentials. - */ - public static NetworkCredentialsParcelable forThread(ThreadCredentials threadCredentials) { - return new NetworkCredentialsParcelable(null, threadCredentials); - } - - public WiFiCredentials getWiFiCredentials() { - return wifiCredentials; - } - - public ThreadCredentials getThreadCredentials() { - return threadCredentials; - } - - // Begin Parcelable implementation - - private NetworkCredentialsParcelable(Parcel in) { - wifiCredentials = in.readParcelable(WiFiCredentials.class.getClassLoader()); - threadCredentials = in.readParcelable(ThreadCredentials.class.getClassLoader()); - } - - public int describeContents() { - return 0; - } - - public void writeToParcel(Parcel out, int flags) { - out.writeParcelable(wifiCredentials, 0); - out.writeParcelable(threadCredentials, 0); - } - - public static final Parcelable.Creator CREATOR = - new Parcelable.Creator() { - public NetworkCredentialsParcelable createFromParcel(Parcel in) { - return new NetworkCredentialsParcelable(in); - } - - public NetworkCredentialsParcelable[] newArray(int size) { - return new NetworkCredentialsParcelable[size]; - } - }; - - public static class WiFiCredentials implements Parcelable { - private final String ssid; - private final String password; - - public WiFiCredentials(String ssid, String password) { - this.ssid = ssid; - this.password = password; - } - - public String getSsid() { - return ssid; - } - - public String getPassword() { - return password; - } - - // Begin Parcelable implementation - - private WiFiCredentials(Parcel in) { - ssid = in.readString(); - password = in.readString(); - } - - public int describeContents() { - return 0; - } - - public void writeToParcel(Parcel out, int flags) { - out.writeString(ssid); - out.writeString(password); - } - - public static final Parcelable.Creator CREATOR = - new Parcelable.Creator() { - public WiFiCredentials createFromParcel(Parcel in) { - return new WiFiCredentials(in); - } - - public WiFiCredentials[] newArray(int size) { - return new WiFiCredentials[size]; - } - }; - } - - public static class ThreadCredentials implements Parcelable { - private final byte[] operationalDataset; - - public ThreadCredentials(byte[] operationalDataset) { - this.operationalDataset = operationalDataset; - } - - public byte[] getOperationalDataset() { - return operationalDataset; - } - - // Begin Parcelable implementation - - private ThreadCredentials(Parcel in) { - operationalDataset = new byte[in.readInt()]; - in.readByteArray(operationalDataset); - } - - public int describeContents() { - return 0; - } - - public void writeToParcel(Parcel out, int flags) { - out.writeInt(operationalDataset.length); - out.writeByteArray(operationalDataset); - } - - public static final Parcelable.Creator CREATOR = - new Parcelable.Creator() { - public ThreadCredentials createFromParcel(Parcel in) { - return new ThreadCredentials(in); - } - - public ThreadCredentials[] newArray(int size) { - return new ThreadCredentials[size]; - } - }; - } -} diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.kt new file mode 100644 index 00000000000000..4f5d5ffafdeb52 --- /dev/null +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/NetworkCredentialsParcelable.kt @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.google.chip.chiptool + +import android.os.Parcelable +import android.os.Parcel +import android.os.Parcelable.Creator + +/** Class for holding WiFi or Thread credentials, but not both. */ +class NetworkCredentialsParcelable : Parcelable { + var wiFiCredentials: WiFiCredentials? + private set + var threadCredentials: ThreadCredentials? + private set + + private constructor( + wifiCredentials: WiFiCredentials?, threadCredentials: ThreadCredentials? + ) { + this.wiFiCredentials = wifiCredentials + this.threadCredentials = threadCredentials + } + + // Begin Parcelable implementation + private constructor(parcel: Parcel) { + wiFiCredentials = parcel.readParcelable(WiFiCredentials::class.java.classLoader) + threadCredentials = parcel.readParcelable(ThreadCredentials::class.java.classLoader) + } + + override fun describeContents(): Int { + return 0 + } + + override fun writeToParcel(out: Parcel, flags: Int) { + out.writeParcelable(wiFiCredentials, 0) + out.writeParcelable(threadCredentials, 0) + } + + class WiFiCredentials : Parcelable { + val ssid: String? + val password: String? + + constructor(ssid: String?, password: String?) { + this.ssid = ssid + this.password = password + } + + // Begin Parcelable implementation + private constructor(parcel: Parcel) { + ssid = parcel.readString() + password = parcel.readString() + } + + override fun describeContents(): Int { + return 0 + } + + override fun writeToParcel(out: Parcel, flags: Int) { + out.writeString(ssid) + out.writeString(password) + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): WiFiCredentials? { + return WiFiCredentials(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } + + class ThreadCredentials : Parcelable { + val operationalDataset: ByteArray + + constructor(operationalDataset: ByteArray) { + this.operationalDataset = operationalDataset + } + + // Begin Parcelable implementation + private constructor(parcel: Parcel) { + operationalDataset = ByteArray(parcel.readInt()) + parcel.readByteArray(operationalDataset) + } + + override fun describeContents(): Int { + return 0 + } + + override fun writeToParcel(out: Parcel, flags: Int) { + out.writeInt(operationalDataset.size) + out.writeByteArray(operationalDataset) + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): ThreadCredentials? { + return ThreadCredentials(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } + + companion object { + /** + * Return a NetworkCredentialsParcelable object with the given WiFiCredentials and null + * ThreadCredentials. + */ + fun forWiFi(wifiCredentials: WiFiCredentials?): NetworkCredentialsParcelable { + return NetworkCredentialsParcelable(wifiCredentials, null) + } + + /** + * Return a NetworkCredentialsParcelable object with the given ThreadCredentials and null + * WiFiCredentials. + */ + fun forThread(threadCredentials: ThreadCredentials?): NetworkCredentialsParcelable { + return NetworkCredentialsParcelable(null, threadCredentials) + } + + @JvmField + val CREATOR = object : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): NetworkCredentialsParcelable? { + return NetworkCredentialsParcelable(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } +}