summaryrefslogtreecommitdiff
path: root/android/sdremote/wear/src/main/java/org/libreoffice/impressremote/communication/DataLayerListenerService.java
blob: 5c3046d2520d552fea67501ccfabfebf94463441 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.impressremote.communication;

import static org.libreoffice.impressremote.communication.Commands.*;

import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;

import org.libreoffice.impressremote.R;
import org.libreoffice.impressremote.activity.FullscreenActivity;
import org.libreoffice.impressremote.activity.NotificationActivity;
import org.libreoffice.impressremote.util.SlideShowData;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

public class DataLayerListenerService extends WearableListenerService implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "DataLayerListenerSrvc";

    private static final int NOTIFICATION_ID=1;
    private static final String NOTIFICATION_TITLE="Impress Remote";

    private static GoogleApiClient mGoogleApiClient;

    public DataLayerListenerService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v(TAG, "Created");

        if(null == mGoogleApiClient) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            Log.v(TAG, "GoogleApiClient created");
        }

        if(!mGoogleApiClient.isConnected()){
            mGoogleApiClient.connect();
            Log.v(TAG, "Connecting to GoogleApiClient..");
        }
    }

    @Override
    public void onDestroy() {
        Log.v(TAG, "Destroyed");
        if(null != mGoogleApiClient){
            if(mGoogleApiClient.isConnected()){
                mGoogleApiClient.disconnect();
                Log.v(TAG, "GoogleApiClient disconnected");
            }
        }
        super.onDestroy();
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.v(TAG, "onConnectionSuspended called");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.v(TAG, "onConnectionFailed called");
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.v(TAG, "onConnected called");
    }

    @Override
    public void onPeerConnected(Node peer) {
        super.onPeerConnected(peer);
        Log.v(TAG, "Peer Connected " + peer.getDisplayName());
    }

    @Override
    public void onPeerDisconnected(Node peer) {
        super.onPeerDisconnected(peer);
        Log.v(TAG, "Peer Disconnected " + peer.getDisplayName());
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        try{
            String message=new String(messageEvent.getData(), "UTF-8");
            Log.v(TAG, "onMessageReceived " + messageEvent.getPath()+message);
            if(messageEvent.getPath().equals(COMMAND_PRESENTATION_STOPPED)){
                cancelLocalNotification();
                if(SlideShowData.getInstance().isFullscreen()){
                    Intent aIntent= new Intent(COMMAND_PRESENTATION_STOPPED);
                    LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
                }
            }
            if(messageEvent.getPath().equals(COMMAND_PRESENTATION_PAUSED)){
                Intent aIntent= new Intent(COMMAND_PRESENTATION_PAUSED);
                LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
            }
            if(messageEvent.getPath().equals(COMMAND_PRESENTATION_RESUMED)){
                Intent aIntent= new Intent(COMMAND_PRESENTATION_RESUMED);
                LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
            }
        }catch(UnsupportedEncodingException ignored){

        }
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        Log.v(TAG, "Data Changed");
        for(DataEvent dataEvent: dataEvents) {
            if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
                Log.v(TAG, "Type Changed");
                String path=dataEvent.getDataItem().getUri().getPath();
                if(path.equals(COMMAND_NOTIFY)){
                    DataMapItem dataMapItem = DataMapItem.fromDataItem(dataEvent.getDataItem());
                    final String count = dataMapItem.getDataMap().getString(INTENT_COUNT);
                    SlideShowData.getInstance().setCount(count);
                    if( dataMapItem.getDataMap().getBoolean(INTENT_HAS_ASSET)){
                        Asset asset = dataMapItem.getDataMap().getAsset(INTENT_PREVIEW);
                        SlideShowData.getInstance().setPreview(loadBitmapFromAsset(asset));
                        SlideShowData.getInstance().setHasPreview(true);
                    }else{
                        SlideShowData.getInstance().setHasPreview(false);
                    }
                    notifyActivity();
                }
            }
        }
    }

    private static void sendMessage( final String path) {
        new Thread( new Runnable() {
            @Override
            public void run() {
                if(mGoogleApiClient!=null){
                    NodeApi.GetConnectedNodesResult nodes =
                            Wearable.NodeApi.getConnectedNodes( mGoogleApiClient ).await();
                    for (Node node : nodes.getNodes()) {
                        Log.d(TAG, "SendMessage " + path);
                        Wearable.MessageApi.sendMessage(
                                mGoogleApiClient, node.getId(), path, null).await();
                    }
                }
            }
        }).start();
    }

    private void notifyActivity(){
        if(SlideShowData.getInstance().isFullscreen()){
            broadcastLocalIntent();
        }else{
            sendLocalNotification();
        }
    }

    private void broadcastLocalIntent(){
        Log.v(TAG, "broadcastLocalIntent");
        Intent aIntent= new Intent(INTENT_UPDATE);
        LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
    }

    private void sendLocalNotification() {
        Log.v(TAG, "sendLocalNotification");

        Intent notificationIntent = new Intent(this, NotificationActivity.class);
        PendingIntent notificationPendingIntent = PendingIntent.getActivity(
                this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent startIntent = new Intent(this, FullscreenActivity.class).setAction(Intent.ACTION_VIEW);
        PendingIntent startPendingIntent = PendingIntent.getActivity(this, 0, startIntent, 0);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(NOTIFICATION_TITLE)
                        .setContentText(SlideShowData.getInstance().getCount())
                        .setOngoing(true)
                        .setContentIntent(startPendingIntent);
        if(SlideShowData.getInstance().hasPreview()){
            notificationBuilder.extend(new NotificationCompat.WearableExtender()
                    .setDisplayIntent(notificationPendingIntent).setBackground(SlideShowData.getInstance().getPreview()));
        }else{
            notificationBuilder.extend(new NotificationCompat.WearableExtender()
                    .setDisplayIntent(notificationPendingIntent));
        }

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }

    private void cancelLocalNotification(){
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.cancel(NOTIFICATION_ID);
    }

    public static void commandNext(){
        sendMessage(COMMAND_NEXT);
    }

    public static void commandPrevious(){
        sendMessage(COMMAND_PREVIOUS);
    }

    public static void commandPauseResume(){
        sendMessage(COMMAND_PAUSERESUME);
    }

    public static void commandConnect(){
        sendMessage(COMMAND_CONNECT);
    }

    public Bitmap loadBitmapFromAsset(Asset asset) {
        if (asset == null) {
            throw new IllegalArgumentException("Asset must be non-null");
        }
        ConnectionResult result =
                mGoogleApiClient.blockingConnect(5000, TimeUnit.MILLISECONDS);
        if (!result.isSuccess()) {
            return null;
        }

        InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
                mGoogleApiClient, asset).await().getInputStream();

        if (assetInputStream == null) {
            Log.w(TAG, "Requested an unknown Asset.");
            return null;
        }

        return BitmapFactory.decodeStream(assetInputStream);
    }

}