summaryrefslogtreecommitdiff
path: root/android/sdremote/mobile/src/full/java/org.libreoffice.impressremote/communication/CommunicationServiceWear.java
blob: 9b7545c4df6d5bf3295bfd9e0ece98c4c2b531a8 (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
/*
 * 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 android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Asset;
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.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;

import org.libreoffice.impressremote.util.Intents;

import java.io.ByteArrayOutputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CommunicationServiceWear extends WearableListenerService {

    private static final String TAG = "CommunicationSrvcWear";

    private static final String COMMAND_NEXT="/next";
    private static final String COMMAND_PREVIOUS="/previous";
    private static final String COMMAND_PAUSERESUME="/pauseResume";
    private static final String COMMAND_CONNECT="/connect";
    private static final String COMMAND_APP_PAUSED="/appPaused";
    private static final String COMMAND_PRESENTATION_STOPPED="/wearableStop";
    private static final String COMMAND_PRESENTATION_PAUSED="/pause";
    private static final String COMMAND_PRESENTATION_RESUMED="/resume";

    public static final String COMMAND_NOTIFY="/notification";

    public static final String INTENT_COUNT="COUNT";
    public static final String INTENT_PREVIEW="PREVIEW";
    public static final String INTENT_HAS_ASSET="HASASSET";

    private static GoogleApiClient googleApiClient;

    private static boolean ingoreSync;

    @Override
     public void onCreate(){
        super.onCreate();
        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .build();
        Log.v(TAG, "GoogleApiClient created");
        googleApiClient.connect();
        Log.v(TAG, "Connecting to GoogleApiClient..");

        Intent aIntent= Intents.buildGoogleApiConnectedIntent();
        LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);

    }

    @Override
    public void onDestroy(){
        Log.v(TAG, "onDestroy");
        if(null != googleApiClient){
            notifyWearStop();
            final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
            exec.schedule(new Runnable() {
                @Override
                public void run() {
                    if (googleApiClient != null) {
                        if (googleApiClient.isConnected()) {
                            googleApiClient.disconnect();
                            Log.v(TAG, "GoogleApiClient disconnected");
                        }
                    }
                }
            }, 2, TimeUnit.SECONDS);
    }
        super.onDestroy();
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {

       Log.d(TAG, "onMessageReceived: " + messageEvent.getPath());

        if(messageEvent.getPath().equals(COMMAND_NEXT)){
            Intent aIntent= Intents.buildWearNextIntent();
            LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
        }
        if(messageEvent.getPath().equals(COMMAND_PREVIOUS)){
            Intent aIntent= Intents.buildWearPreviousIntent();
            LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
        }
/*        if(messageEvent.getPath().equals("/pause")){
            Intent aIntent= Intents.buildWearPauseIntent();
            LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
        }
        if(messageEvent.getPath().equals("/resume")){
            Intent aIntent= Intents.buildWearResumeIntent();
            LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
        }*/
        if(messageEvent.getPath().equals(COMMAND_CONNECT)){
            Intent aIntent= Intents.buildWearConnectIntent();
            LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
        }
        if(messageEvent.getPath().equals(COMMAND_APP_PAUSED)){
            Intent aIntent= Intents.buildWearExitIntent();
            LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
        }
        if(messageEvent.getPath().equals(COMMAND_PAUSERESUME)){
            Intent aIntent= Intents.buildWearPauseResumeIntent();
            LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
        }
    }

    @Override
    public void onPeerConnected(Node peer) {
        super.onPeerConnected(peer);
        String id = peer.getId();
        String name = peer.getDisplayName();
        Log.d(TAG, "Connected peer name & ID: " + name + "|" + id);
    }

    private void notifyWearStop(){
        sendMessage(COMMAND_PRESENTATION_STOPPED);
    }
    public static void presentationPaused(){
        sendMessage(COMMAND_PRESENTATION_PAUSED);
    }
    public static void presentationResumed(){
        sendMessage(COMMAND_PRESENTATION_RESUMED);
    }

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

    public static void syncData(final String count,final byte[] preview) {
        if(ingoreSync){
            Log.d(TAG, "Ignoring sync!");
            ingoreSync=false;
        }else{
            new Thread( new Runnable() {
                @Override
                public void run() {
                    if(googleApiClient !=null){
                        Log.d(TAG, "syncData");
                        googleApiClient.connect();
                        if (googleApiClient!=null) {
                            PutDataMapRequest dataMapRequest = PutDataMapRequest.create(COMMAND_NOTIFY);

                            dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
                            dataMapRequest.getDataMap().putString(INTENT_COUNT, count);
                            if (preview == null) {
                                dataMapRequest.getDataMap().putBoolean(INTENT_HAS_ASSET,false);
                            }else{
                                Asset asset = createAssetFromByte(preview);
                                dataMapRequest.getDataMap().putBoolean(INTENT_HAS_ASSET,true);
                                dataMapRequest.getDataMap().putAsset(INTENT_PREVIEW, asset);
                            }
                            PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
                            Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
                        }
                        else {
                            Log.e(TAG, "No connection to wearable available!");
                        }
                    }
                }
            }).start();
        }

    }

    public static void ignoreNextSync(){
        ingoreSync=true;
    }

    private static Asset createAssetFromByte(byte[] data){
        if(data==null){
            throw new IllegalArgumentException("Null byte array.");
        }
        Bitmap bitmap =BitmapFactory.decodeByteArray(data, 0, data.length);
        Bitmap.createScaledBitmap(bitmap,150,150,false);
        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
        return Asset.createFromBytes(byteStream.toByteArray());
    }

}