Skip to content

Commit 1bf4091

Browse files
スマホに表示されているActivityの監視ロジックの追加。
1 parent 7639ebb commit 1bf4091

5 files changed

Lines changed: 73 additions & 58 deletions

File tree

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/HostDeviceApplication.java

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
*/
77
package org.deviceconnect.android.deviceplugin.host;
88

9+
import android.app.Activity;
910
import android.app.Application;
1011
import android.content.Intent;
1112
import android.os.Bundle;
13+
import android.util.Log;
14+
15+
import androidx.annotation.NonNull;
16+
import androidx.annotation.Nullable;
1217

1318
import org.deviceconnect.android.logger.AndroidHandler;
1419
import org.deviceconnect.android.profile.BatteryProfile;
@@ -31,7 +36,7 @@
3136
*
3237
* @author NTT DOCOMO, INC.
3338
*/
34-
public class HostDeviceApplication extends Application {
39+
public class HostDeviceApplication extends Application implements Application.ActivityLifecycleCallbacks{
3540

3641
/** Cache retention time (mSec). */
3742
static final long CACHE_RETENTION_TIME = 10000;
@@ -192,6 +197,9 @@ public void setTouchCache(final String attr, final Bundle touchData) {
192197
public static final String STATE_UP = "up";
193198
/** KeyEvent State cancel. */
194199
public static final String STATE_DOWN = "down";
200+
201+
/** 現在表示されているActivity名. */
202+
private String mNowTopActivityClassName = "";
195203
/**
196204
* Get KeyEvent cache data.
197205
*
@@ -223,6 +231,14 @@ public Bundle getKeyEventCache(final String attr) {
223231
}
224232
}
225233

234+
/**
235+
* HostプラグインがサポートしているActivity(Manager含む)の名前を返す.
236+
* @return 現在表示されているActivity名
237+
*/
238+
public String getClassnameOfTopActivity() {
239+
return mNowTopActivityClassName;
240+
}
241+
226242
/**
227243
* Set KeyEvent data to cache.
228244
*
@@ -256,12 +272,45 @@ public void onCreate() {
256272
logger.setLevel(Level.ALL);
257273
} else {
258274
logger.setLevel(Level.OFF);
259-
logger.setFilter(new Filter() {
260-
@Override
261-
public boolean isLoggable(final LogRecord record) {
262-
return false;
263-
}
264-
});
275+
logger.setFilter((record) -> false);
265276
}
277+
registerActivityLifecycleCallbacks(this);
278+
}
279+
280+
@Override
281+
public void onTerminate() {
282+
unregisterActivityLifecycleCallbacks(this);
283+
super.onTerminate();
284+
}
285+
@Override
286+
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
287+
}
288+
289+
@Override
290+
public void onActivityStarted(@NonNull Activity activity) {
291+
mNowTopActivityClassName = activity.getLocalClassName();
292+
}
293+
294+
@Override
295+
public void onActivityResumed(@NonNull Activity activity) {
296+
mNowTopActivityClassName = activity.getLocalClassName();
297+
}
298+
299+
@Override
300+
public void onActivityPaused(@NonNull Activity activity) {
301+
}
302+
303+
@Override
304+
public void onActivityStopped(@NonNull Activity activity) {
305+
mNowTopActivityClassName = "";
306+
}
307+
308+
@Override
309+
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {
310+
}
311+
312+
@Override
313+
public void onActivityDestroyed(@NonNull Activity activity) {
314+
mNowTopActivityClassName = "";
266315
}
267316
}

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/mediaplayer/HostMediaPlayerManager.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import android.os.Bundle;
2323
import android.os.Handler;
2424
import android.provider.MediaStore;
25+
import android.util.Log;
2526
import android.webkit.MimeTypeMap;
2627

2728
import org.deviceconnect.android.deviceplugin.host.BuildConfig;
29+
import org.deviceconnect.android.deviceplugin.host.HostDeviceApplication;
2830
import org.deviceconnect.android.deviceplugin.host.HostDevicePlugin;
2931
import org.deviceconnect.android.event.Event;
3032
import org.deviceconnect.android.event.EventManager;
@@ -461,15 +463,14 @@ public int playMedia(final Intent response) {
461463
return mMediaPlayer.getAudioSessionId();
462464
} else if (mSetMediaType == MEDIA_TYPE_VIDEO) {
463465
mHostDevicePluginContext.getContext().registerReceiver(mMediaPlayerVideoBR, mIfMediaPlayerVideo);
464-
String className = getClassnameOfTopActivity();
466+
String className = getApp().getClassnameOfTopActivity();
465467

466468
if (VideoPlayer.class.getName().equals(className)) {
467469
mMediaStatus = MEDIA_PLAYER_PLAY;
468470
Intent mIntent = new Intent(VideoConst.SEND_HOSTDP_TO_VIDEOPLAYER);
469471
mIntent.putExtra(VideoConst.EXTRA_NAME, VideoConst.EXTRA_VALUE_VIDEO_PLAYER_PLAY);
470472
getContext().sendBroadcast(mIntent);
471473
sendOnStatusChangeEvent("play");
472-
473474
} else {
474475
mMediaStatus = MEDIA_PLAYER_PLAY;
475476
Intent mIntent = new Intent(VideoConst.SEND_HOSTDP_TO_VIDEOPLAYER);
@@ -568,7 +569,7 @@ public int getMediaPos() {
568569
if (mSetMediaType == MEDIA_TYPE_MUSIC) {
569570
return mMediaPlayer.getCurrentPosition() / UNIT_SEC;
570571
} else if (mSetMediaType == MEDIA_TYPE_VIDEO) {
571-
String className = getClassnameOfTopActivity();
572+
String className = getApp().getClassnameOfTopActivity();
572573
if (VideoPlayer.class.getName().equals(className)) {
573574
Intent mIntent = new Intent(VideoConst.SEND_HOSTDP_TO_VIDEOPLAYER);
574575
mIntent.putExtra(VideoConst.EXTRA_NAME, VideoConst.EXTRA_VALUE_VIDEO_PLAYER_GET_POS);
@@ -719,7 +720,7 @@ public void stopMedia(final Intent response) {
719720
* @param response レスポンス
720721
*/
721722
public void getPlayStatus(final Intent response) {
722-
String mClassName = getClassnameOfTopActivity();
723+
String mClassName = getApp().getClassnameOfTopActivity();
723724

724725
// VideoRecorderの場合は、画面から消えている場合
725726
if (mSetMediaType == MEDIA_TYPE_VIDEO) {
@@ -793,14 +794,8 @@ private String getMIMEType(final String path) {
793794
mExt = mExt.toLowerCase(Locale.getDefault());
794795
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(mExt);
795796
}
796-
797-
/**
798-
* 画面の一番上にでているActivityのクラス名を取得.
799-
*
800-
* @return クラス名
801-
*/
802-
private String getClassnameOfTopActivity() {
803-
ActivityManager mActivityManager = (ActivityManager) getContext().getSystemService(Service.ACTIVITY_SERVICE);
804-
return mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName();
797+
private HostDeviceApplication getApp() {
798+
return (HostDeviceApplication) getContext().getApplicationContext();
805799
}
800+
806801
}

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostCanvasProfile.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77

88
package org.deviceconnect.android.deviceplugin.host.profile;
99

10-
import android.app.ActivityManager;
11-
import android.app.Service;
1210
import android.content.Intent;
1311
import android.os.Build;
1412
import androidx.annotation.NonNull;
1513
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
1614

15+
import org.deviceconnect.android.deviceplugin.host.HostDeviceApplication;
1716
import org.deviceconnect.android.deviceplugin.host.activity.CanvasProfileActivity;
1817
import org.deviceconnect.android.deviceplugin.host.canvas.CanvasDrawImageObject;
1918
import org.deviceconnect.android.message.MessageUtils;
@@ -113,7 +112,7 @@ public String getAttribute() {
113112

114113
@Override
115114
public boolean onRequest(final Intent request, final Intent response) {
116-
String className = getClassnameOfTopActivity();
115+
String className = getApp().getClassnameOfTopActivity();
117116
if (CanvasProfileActivity.class.getName().equals(className)) {
118117
Intent intent = new Intent(CanvasDrawImageObject.ACTION_DELETE_CANVAS);
119118
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
@@ -164,7 +163,7 @@ private void sendImage(byte[] data, Intent response, CanvasDrawImageObject.Mode
164163
private void drawImage(Intent response, String uri, CanvasDrawImageObject.Mode enumMode, double x, double y) {
165164
CanvasDrawImageObject drawObj = new CanvasDrawImageObject(uri, enumMode, x, y);
166165

167-
String className = getClassnameOfTopActivity();
166+
String className = getApp().getClassnameOfTopActivity();
168167
if (CanvasProfileActivity.class.getName().equals(className)) {
169168
Intent intent = new Intent(CanvasDrawImageObject.ACTION_DRAW_CANVAS);
170169
drawObj.setValueToIntent(intent);
@@ -185,14 +184,8 @@ private void drawImage(Intent response, String uri, CanvasDrawImageObject.Mode e
185184
setResult(response, DConnectMessage.RESULT_OK);
186185
}
187186

188-
/**
189-
* 画面の一番上にでているActivityのクラス名を取得.
190-
*
191-
* @return クラス名
192-
*/
193-
private String getClassnameOfTopActivity() {
194-
ActivityManager activityMgr = (ActivityManager) getContext().getSystemService(Service.ACTIVITY_SERVICE);
195-
return activityMgr.getRunningTasks(1).get(0).topActivity.getClassName();
187+
private HostDeviceApplication getApp() {
188+
return (HostDeviceApplication) getContext().getApplicationContext();
196189
}
197190

198191
/**

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostKeyEventProfile.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ public HostKeyEventProfile() {
295295
* @return Always true.
296296
*/
297297
private boolean execKeyEventActivity(final String serviceId) {
298-
ActivityManager mActivityManager = (ActivityManager) getContext().getSystemService(Service.ACTIVITY_SERVICE);
299-
String mClassName = mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName();
298+
String mClassName = getApp().getClassnameOfTopActivity();
300299

301300
if (!(KeyEventProfileActivity.class.getName().equals(mClassName))) {
302301
Intent mIntent = new Intent();
@@ -319,7 +318,7 @@ private boolean execKeyEventActivity(final String serviceId) {
319318
* @return Always true.
320319
*/
321320
private boolean finishKeyEventProfileActivity() {
322-
String className = getClassnameOfTopActivity();
321+
String className = getApp().getClassnameOfTopActivity();
323322
if (KeyEventProfileActivity.class.getName().equals(className)) {
324323
Intent intent = new Intent(HostKeyEventProfile.ACTION_FINISH_KEYEVENT_ACTIVITY);
325324
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
@@ -348,16 +347,6 @@ private void resetKeyEventEventFlag(final int flag) {
348347
}
349348
}
350349

351-
/**
352-
* Get the class name of the Activity being displayed at the top of the screen.
353-
*
354-
* @return class name.
355-
*/
356-
private String getClassnameOfTopActivity() {
357-
ActivityManager activityMgr = (ActivityManager) getContext().getSystemService(Service.ACTIVITY_SERVICE);
358-
return activityMgr.getRunningTasks(1).get(0).topActivity.getClassName();
359-
}
360-
361350
/**
362351
* Check set KeyEvent event manage flag.
363352
*

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostTouchProfile.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,7 @@ public HostTouchProfile() {
582582
* @return Always true.
583583
*/
584584
private boolean execTouchProfileActivity(final String serviceId) {
585-
ActivityManager mActivityManager = (ActivityManager) getContext().getSystemService(Service.ACTIVITY_SERVICE);
586-
String mClassName = mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName();
585+
String mClassName = getApp().getClassnameOfTopActivity();
587586

588587
if (!(TouchProfileActivity.class.getName().equals(mClassName))) {
589588
Intent mIntent = new Intent();
@@ -606,7 +605,7 @@ private boolean execTouchProfileActivity(final String serviceId) {
606605
* @return Always true.
607606
*/
608607
private boolean finishTouchProfileActivity() {
609-
String className = getClassnameOfTopActivity();
608+
String className = getApp().getClassnameOfTopActivity();
610609
if (TouchProfileActivity.class.getName().equals(className)) {
611610
Intent intent = new Intent(HostTouchProfile.ACTION_FINISH_TOUCH_ACTIVITY);
612611
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
@@ -635,16 +634,6 @@ private void resetTouchEventFlag(final int flag) {
635634
}
636635
}
637636

638-
/**
639-
* Get the class name of the Activity being displayed at the top of the screen.
640-
*
641-
* @return class name.
642-
*/
643-
private String getClassnameOfTopActivity() {
644-
ActivityManager activityMgr = (ActivityManager) getContext().getSystemService(Service.ACTIVITY_SERVICE);
645-
return activityMgr.getRunningTasks(1).get(0).topActivity.getClassName();
646-
}
647-
648637
/**
649638
* Check set Touch event manage flag.
650639
*

0 commit comments

Comments
 (0)