wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
/*  Copyright (C) 2021-2024 Andreas Shimokawa, Arjan Schrijver, Gordon
    Williams, José Rebelo
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.externalevents;
 
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.RemoteException;
import android.view.KeyEvent;
 
import net.osmand.aidlapi.IOsmAndAidlCallback;
import net.osmand.aidlapi.IOsmAndAidlInterface;
import net.osmand.aidlapi.gpx.AGpxBitmap;
import net.osmand.aidlapi.navigation.ADirectionInfo;
import net.osmand.aidlapi.navigation.ANavigationUpdateParams;
import net.osmand.aidlapi.navigation.ANavigationVoiceRouterMessageParams;
import net.osmand.aidlapi.navigation.OnVoiceNavigationParams;
import net.osmand.aidlapi.search.SearchResult;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.List;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.model.NavigationInfoSpec;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
 
public class OsmandEventReceiver {
    private static final Logger LOG = LoggerFactory.getLogger(OsmandEventReceiver.class);
 
    private final Application app;
    private IOsmAndAidlInterface mIOsmAndAidlInterface;
 
    private NavigationInfoSpec navigationInfoSpec = new NavigationInfoSpec();
 
    private final IOsmAndAidlCallback.Stub mIOsmAndAidlCallback = new IOsmAndAidlCallback.Stub() {
        @Override
        public void onSearchComplete(List<SearchResult> resultSet) {
        }
 
        @Override
        public void onUpdate() {
        }
 
        @Override
        public void onAppInitialized() {
        }
 
        @Override
        public void onGpxBitmapCreated(AGpxBitmap bitmap) {
        }
 
        @Override
        public void updateNavigationInfo(ADirectionInfo directionInfo) {
            navigationInfoSpec.nextAction = directionInfo.getTurnType();
            navigationInfoSpec.distanceToTurn = directionInfo.getDistanceTo()+"m";
 
            if (shouldSendNavigation()) {
                GBApplication.deviceService().onSetNavigationInfo(navigationInfoSpec);
            }
 
            LOG.debug("Distance: {}, turnType: {}", directionInfo.getDistanceTo(), directionInfo.getTurnType());
        }
 
        @Override
        public void onContextMenuButtonClicked(int buttonId, String pointId, String layerId) {
        }
 
        @Override
        public void onVoiceRouterNotify(OnVoiceNavigationParams params) {
            List<String> played = params.getPlayed();
            for (String instuction : played) {
                navigationInfoSpec.instruction = instuction;
                LOG.debug("instruction: {}", instuction);
                // only first one for now
                break;
            }
        }
 
        @Override
        public void onKeyEvent(KeyEvent keyEvent) {
 
        }
    };
 
    private final ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service);
            LOG.info("OsmAnd service connected");
            registerForNavigationUpdates(true, 6666); // what is this id for?
            registerForVoiceRouterMessages(true, 6667);
//            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("osmand.api://GET_INFO"));
//            OsmandEventReceiver.this.startActivityForResult(intent,666);
        }
 
        public void onServiceDisconnected(ComponentName className) {
            mIOsmAndAidlInterface = null;
            LOG.info("OsmAnd service disconnected");
        }
    };
 
    public OsmandEventReceiver(Application application) {
        this.app = application;
        if (!bindService()) {
            LOG.warn("Could not bind to OsmAnd");
        }
    }
 
    private boolean bindService() {
        if (mIOsmAndAidlInterface == null) {
            List<CharSequence> installedOsmandPackages = findInstalledOsmandPackages();
            if (installedOsmandPackages.isEmpty()) {
                LOG.warn("OsmAnd is not installed");
                return false;
            }
            Prefs prefs = GBApplication.getPrefs();
            String packageName = prefs.getString("pref_key_osmand_packagename", "autodetect");
            if (packageName.equals("autodetect")) packageName = installedOsmandPackages.get(0).toString();
            Intent intent = new Intent("net.osmand.aidl.OsmandAidlServiceV2");
            intent.setPackage(packageName);
            boolean res = app.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
            if (res) {
                LOG.info("Bound to OsmAnd service (package "+packageName+")");
                return true;
            } else {
                LOG.warn("Could not bind to OsmAnd service (package "+packageName+")");
                return false;
            }
        } else {
            return true;
        }
    }
 
    public void cleanupResources() {
        if (mIOsmAndAidlInterface != null) {
            app.unbindService(mConnection);
        }
    }
 
    public long registerForNavigationUpdates(boolean subscribeToUpdates, long callbackId) {
        if (mIOsmAndAidlInterface != null) {
            try {
                ANavigationUpdateParams params = new ANavigationUpdateParams();
                params.setCallbackId(callbackId);
                params.setSubscribeToUpdates(subscribeToUpdates);
 
                return mIOsmAndAidlInterface.registerForNavigationUpdates(params, mIOsmAndAidlCallback);
            } catch (RemoteException e) {
                LOG.error("could not subscribe to navigation updates", e);
            }
        }
        return -1L;
    }
 
    private boolean shouldSendNavigation() {
        Prefs prefs = GBApplication.getPrefs();
 
        boolean navigationForward = prefs.getBoolean("navigation_forward", true);
        boolean navigationOsmAnd = prefs.getBoolean("navigation_app_osmand", true);
        if (!navigationForward || !navigationOsmAnd) {
            return false;
        }
 
        boolean navigationScreenOn = prefs.getBoolean("nagivation_screen_on", true);
        if (!navigationScreenOn) {
            PowerManager powermanager = (PowerManager) app.getSystemService(Context.POWER_SERVICE);
            if (powermanager != null && powermanager.isScreenOn()) {
                LOG.info("Not forwarding navigation instructions, screen seems to be on and settings do not allow this");
                return false;
            }
        }
 
        return true;
    }
 
    /**
     * Method to register for Voice Router voice messages during navigation. Notifies user about voice messages.
     *
     * @param subscribeToUpdates (boolean) - boolean flag to subscribe or unsubscribe from messages
     * @param callbackId         (long) - id of callback, needed to unsubscribe from messages
     */
    public long registerForVoiceRouterMessages(boolean subscribeToUpdates, long callbackId) {
        ANavigationVoiceRouterMessageParams params = new ANavigationVoiceRouterMessageParams();
        params.setCallbackId(callbackId);
        params.setSubscribeToUpdates(subscribeToUpdates);
        if (mIOsmAndAidlInterface != null) {
            try {
                return mIOsmAndAidlInterface.registerForVoiceRouterMessages(params, mIOsmAndAidlCallback);
            } catch (RemoteException e) {
                LOG.error("could not register for voice router messages", e);
            }
        }
        return -1L;
    }
 
    public List<CharSequence> findInstalledOsmandPackages() {
        List<CharSequence> installedPackages = new ArrayList<>();
        for (String knownPackage : app.getBaseContext().getResources().getStringArray(R.array.osmand_package_names)) {
            if (isPackageInstalled(knownPackage)) {
                installedPackages.add(knownPackage);
            }
        }
        return installedPackages;
    }
 
    private boolean isPackageInstalled(final String packageName) {
        try {
            return app.getBaseContext().getPackageManager().getApplicationInfo(packageName, 0).enabled;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}