diff --git a/examples/tv-app/android/App/app/src/main/AndroidManifest.xml b/examples/tv-app/android/App/app/src/main/AndroidManifest.xml index 383830aaf0b8b9..a51122c976037d 100644 --- a/examples/tv-app/android/App/app/src/main/AndroidManifest.xml +++ b/examples/tv-app/android/App/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ + - + + \ No newline at end of file diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/ChipTvServerApplication.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/ChipTvServerApplication.java new file mode 100644 index 00000000000000..79a13d9a0cdf18 --- /dev/null +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/ChipTvServerApplication.java @@ -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); + } + } +} diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java index 41014fe7f100ea..7475aa4e7e8e85 100644 --- a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/MainActivity.java @@ -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 { @@ -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 discoveryCapabilities = new HashSet<>(); @@ -78,8 +47,5 @@ protected void onCreate(Bundle savedInstanceState) { } catch (SetupPayloadParser.SetupPayloadException e) { e.printStackTrace(); } - - ChipAppServer chipAppServer = new ChipAppServer(); - chipAppServer.startApp(); } } diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java new file mode 100644 index 00000000000000..aa1cf46ec00903 --- /dev/null +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java @@ -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(); + } +} diff --git a/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServantService.java b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServantService.java new file mode 100644 index 00000000000000..a65ebffa505aa2 --- /dev/null +++ b/examples/tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServantService.java @@ -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(); + } +}