Skip to content

Commit

Permalink
move ChipAppServer to a android service (#12313)
Browse files Browse the repository at this point in the history
* move ChipAppServer to a android service

* Restyled by google-java-format

* Restyled by google-java-format

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed May 14, 2022
1 parent b4b937b commit 2996a6f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 35 deletions.
6 changes: 5 additions & 1 deletion examples/tv-app/android/App/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

<application
Expand All @@ -18,14 +19,17 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".ChipTvServerApplication"
android:theme="@style/Theme.CHIPTVSlave">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.MatterServantService" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tcl.chip.chiptvserver;

import android.app.Application;
import android.content.Intent;
import android.os.Build;
import com.tcl.chip.chiptvserver.service.MatterServantService;

public class ChipTvServerApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
startMatterServantService();
}

private void startMatterServantService() {
Intent intent = new Intent(this, MatterServantService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getApplicationContext().startForegroundService(intent);
} else {
startService(intent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,9 @@
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import chip.appserver.ChipAppServer;
import chip.platform.AndroidBleManager;
import chip.platform.AndroidChipPlatform;
import chip.platform.ChipMdnsCallbackImpl;
import chip.platform.NsdManagerServiceResolver;
import chip.platform.PreferencesConfigurationManager;
import chip.platform.PreferencesKeyValueStoreManager;
import chip.setuppayload.DiscoveryCapability;
import chip.setuppayload.SetupPayload;
import chip.setuppayload.SetupPayloadParser;
import com.tcl.chip.tvapp.ContentLaunchManagerStub;
import com.tcl.chip.tvapp.KeypadInputManagerStub;
import com.tcl.chip.tvapp.LowPowerManagerStub;
import com.tcl.chip.tvapp.MediaInputManagerStub;
import com.tcl.chip.tvapp.MediaPlaybackManagerStub;
import com.tcl.chip.tvapp.TvApp;
import com.tcl.chip.tvapp.TvChannelManagerStub;
import com.tcl.chip.tvapp.WakeOnLanManagerStub;
import java.util.HashSet;

public class MainActivity extends AppCompatActivity {
Expand All @@ -38,22 +23,6 @@ protected void onCreate(Bundle savedInstanceState) {
mQrCodeImg = findViewById(R.id.qrCodeImg);
mQrCodeTxt = findViewById(R.id.qrCodeTxt);
mManualPairingCodeTxt = findViewById(R.id.manualPairingCodeTxt);
TvApp tvApp = new TvApp();
tvApp.setKeypadInputManager(new KeypadInputManagerStub());
tvApp.setWakeOnLanManager(new WakeOnLanManagerStub());
tvApp.setMediaInputManager(new MediaInputManagerStub());
tvApp.setContentLaunchManager(new ContentLaunchManagerStub());
tvApp.setLowPowerManager(new LowPowerManagerStub());
tvApp.setMediaPlaybackManager(new MediaPlaybackManagerStub());
tvApp.setTvChannelManager(new TvChannelManagerStub());

AndroidChipPlatform chipPlatform =
new AndroidChipPlatform(
new AndroidBleManager(),
new PreferencesKeyValueStoreManager(this),
new PreferencesConfigurationManager(this),
new NsdManagerServiceResolver(this),
new ChipMdnsCallbackImpl());

// TODO: Get these parameters from PreferencesConfigurationManager
HashSet<DiscoveryCapability> discoveryCapabilities = new HashSet<>();
Expand All @@ -78,8 +47,5 @@ protected void onCreate(Bundle savedInstanceState) {
} catch (SetupPayloadParser.SetupPayloadException e) {
e.printStackTrace();
}

ChipAppServer chipAppServer = new ChipAppServer();
chipAppServer.startApp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.tcl.chip.chiptvserver.service;

import android.content.Context;
import androidx.annotation.NonNull;
import chip.appserver.ChipAppServer;
import chip.platform.AndroidBleManager;
import chip.platform.AndroidChipPlatform;
import chip.platform.ChipMdnsCallbackImpl;
import chip.platform.NsdManagerServiceResolver;
import chip.platform.PreferencesConfigurationManager;
import chip.platform.PreferencesKeyValueStoreManager;
import com.tcl.chip.tvapp.ContentLaunchManagerStub;
import com.tcl.chip.tvapp.KeypadInputManagerStub;
import com.tcl.chip.tvapp.LowPowerManagerStub;
import com.tcl.chip.tvapp.MediaInputManagerStub;
import com.tcl.chip.tvapp.MediaPlaybackManagerStub;
import com.tcl.chip.tvapp.TvApp;
import com.tcl.chip.tvapp.TvChannelManagerStub;
import com.tcl.chip.tvapp.WakeOnLanManagerStub;

public class MatterServant {

private MatterServant() {}

private static class SingletonHolder {
static MatterServant instance = new MatterServant();
}

public static MatterServant get() {
return SingletonHolder.instance;
}

public void init(@NonNull Context context) {
TvApp tvApp = new TvApp();
tvApp.setKeypadInputManager(new KeypadInputManagerStub());
tvApp.setWakeOnLanManager(new WakeOnLanManagerStub());
tvApp.setMediaInputManager(new MediaInputManagerStub());
tvApp.setContentLaunchManager(new ContentLaunchManagerStub());
tvApp.setLowPowerManager(new LowPowerManagerStub());
tvApp.setMediaPlaybackManager(new MediaPlaybackManagerStub());
tvApp.setTvChannelManager(new TvChannelManagerStub());

Context applicationContext = context.getApplicationContext();
AndroidChipPlatform chipPlatform =
new AndroidChipPlatform(
new AndroidBleManager(),
new PreferencesKeyValueStoreManager(applicationContext),
new PreferencesConfigurationManager(applicationContext),
new NsdManagerServiceResolver(applicationContext),
new ChipMdnsCallbackImpl());

ChipAppServer chipAppServer = new ChipAppServer();
chipAppServer.startApp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.tcl.chip.chiptvserver.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.tcl.chip.chiptvserver.MainActivity;
import com.tcl.chip.chiptvserver.R;

public class MatterServantService extends Service {
private static final String CHANNEL_ID = "Matter";

@Override
public void onCreate() {
super.onCreate();
MatterServant.get().init(this.getApplicationContext());
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("MatterServant Service")
.setContentText("MatterServant is running")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
return super.onStartCommand(intent, flags, startId);
}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel =
new NotificationChannel(
CHANNEL_ID, "Matter Servant", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}

@Override
public void onDestroy() {
super.onDestroy();
}
}

0 comments on commit 2996a6f

Please sign in to comment.