-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move ChipAppServer to a android service (#12313)
* move ChipAppServer to a android service * Restyled by google-java-format * Restyled by google-java-format Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
Showing
5 changed files
with
144 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/ChipTvServerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...tv-app/android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...android/App/app/src/main/java/com/tcl/chip/chiptvserver/service/MatterServantService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |