-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewService.java
77 lines (62 loc) · 2.5 KB
/
ViewService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.example.watchviewstubissue;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowManager;
/**
* Created by nicolas on 04/08/14.
*/
public class ViewService extends Service {
private WindowManager windowManager;
private View mainView;
private String formFactor;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate() {
Log.d("ViewService", "Starting service");
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainView = layoutInflater.inflate(R.layout.activity_main, null);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN, PixelFormat.TRANSPARENT);
params.gravity = Gravity.TOP|Gravity.RIGHT;
windowManager.addView(mainView, params);
final WatchViewStub stub = (WatchViewStub) mainView.findViewById(R.id.watch_view_stub);
mainView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isRound()) {
Log.d("ViewService", "Round");
} else {
Log.d("ViewService", "Square");
}
return insets;
}
});
stub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopSelf();
}
});
}
@Override
public void onDestroy() {
windowManager.removeView(mainView);
super.onDestroy();
}
}