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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*  Copyright (C) 2016-2024 Andreas Shimokawa, Arjan Schrijver, Carsten
    Pfeiffer, Daniel Dakhno, Daniele Gobbetti, Felix Konstantin Maurer, José
    Rebelo, Marc Nause, Petr Vaněk, Taavi Eomäe
 
    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.util;
 
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelUuid;
import android.os.Parcelable;
import android.os.PowerManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.widget.Toast;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
import com.google.android.material.color.DynamicColors;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Locale;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
 
public class AndroidUtils {
    private static final Logger LOG = LoggerFactory.getLogger(AndroidUtils.class);
 
    /**
     * Creates a new {@link ParcelUuid} array with the contents of the given uuids.
     * The given array is expected to contain only {@link ParcelUuid} elements.
     * @param uuids an array of {@link ParcelUuid} elements
     * @return a {@link ParcelUuid} array instance with the same contents
     */
    public static ParcelUuid[] toParcelUuids(Parcelable[] uuids) {
        if (uuids == null) {
            return null;
        }
        ParcelUuid[] uuids2 = new ParcelUuid[uuids.length];
        System.arraycopy(uuids, 0, uuids2, 0, uuids.length);
        return uuids2;
    }
 
    /**
     * Unregisters the given receiver from the given context.
     * @param context the context from which to unregister
     * @param receiver the receiver to unregister
     * @return true if it was successfully unregistered, or false if the receiver was not registered
     */
    public static boolean safeUnregisterBroadcastReceiver(Context context, BroadcastReceiver receiver) {
        try {
            context.unregisterReceiver(receiver);
            return true;
        } catch (IllegalArgumentException ex) {
            return false;
        }
    }
 
    /**
     * Unregisters the given receiver from the given {@link LocalBroadcastManager}.
     * @param manager the manager  from which to unregister
     * @param receiver the receiver to unregister
     * @return true if it was successfully unregistered, or false if the receiver was not registered
     */
    public static boolean safeUnregisterBroadcastReceiver(LocalBroadcastManager manager, BroadcastReceiver receiver) {
        try {
            manager.unregisterReceiver(receiver);
            return true;
        } catch (IllegalArgumentException ex) {
            return false;
        }
    }
 
    public static void setLanguage(Context context, Locale language) {
        Configuration config = new Configuration();
        config.setLocale(language);
 
        // FIXME: I have no idea what I am doing
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
 
        Locale.setDefault(language);
    }
 
    /**
     * Returns the theme dependent text color as a css-style hex string.
     * @param context the context to access the colour
     */
    public static String getTextColorHex(Context context) {
        int color;
        if (DynamicColors.isDynamicColorAvailable() && GBApplication.areDynamicColorsEnabled()) {
            Context dynamicColorContext;
            if (GBApplication.isDarkThemeEnabled()) {
                dynamicColorContext = DynamicColors.wrapContextIfAvailable(context, R.style.GadgetbridgeThemeDynamicDark);
            } else {
                dynamicColorContext = DynamicColors.wrapContextIfAvailable(context, R.style.GadgetbridgeThemeDynamicLight);
            }
            int[] attrsToResolve = {com.google.android.material.R.attr.colorOnSurface};
            @SuppressLint("ResourceType")
            TypedArray ta = dynamicColorContext.obtainStyledAttributes(attrsToResolve);
            color = ta.getColor(0, 0);
            ta.recycle();
        } else if (GBApplication.isDarkThemeEnabled()) {
            color = context.getResources().getColor(R.color.primarytext_dark);
        } else {
            color = context.getResources().getColor(R.color.primarytext_light);
        }
        return colorToHex(color);
    }
 
    /**
     * Returns the theme dependent background color as a css-style hex string.
     * @param context the context to access the colour
     */
    public static String getBackgroundColorHex(Context context) {
        int color;
        if (DynamicColors.isDynamicColorAvailable() && GBApplication.areDynamicColorsEnabled()) {
            Context dynamicColorContext;
            if (GBApplication.isDarkThemeEnabled()) {
                dynamicColorContext = DynamicColors.wrapContextIfAvailable(context, R.style.GadgetbridgeThemeDynamicDark);
            } else {
                dynamicColorContext = DynamicColors.wrapContextIfAvailable(context, R.style.GadgetbridgeThemeDynamicLight);
            }
            int[] attrsToResolve = {com.google.android.material.R.attr.colorSurface};
            @SuppressLint("ResourceType")
            TypedArray ta = dynamicColorContext.obtainStyledAttributes(attrsToResolve);
            color = ta.getColor(0, 0);
            ta.recycle();
        } else if (GBApplication.isDarkThemeEnabled()) {
            color = context.getResources().getColor(androidx.cardview.R.color.cardview_dark_background);
        } else {
            color = context.getResources().getColor(androidx.cardview.R.color.cardview_light_background);
        }
        return colorToHex(color);
    }
 
    public static int getBackgroundColor(Context context) {
        int color;
        if (GBApplication.isDarkThemeEnabled()) {
            color = context.getResources().getColor(androidx.cardview.R.color.cardview_dark_background);
        } else {
            color = context.getResources().getColor(androidx.cardview.R.color.cardview_light_background);
        }
        return color;
    }
 
 
    private static String colorToHex(int color) {
        return "#"
                + Integer.toHexString(Color.red(color))
                + Integer.toHexString(Color.green(color))
                + Integer.toHexString(Color.blue(color));
    }
 
    /**
     * As seen on StackOverflow https://stackoverflow.com/a/36714242/1207186
     * Try to find the file path of a document uri
     *
     * @param context the application context
     * @param uri     the Uri for which the path should be resolved
     * @return the path corresponding to the Uri as a String
     * @throws IllegalArgumentException on any problem decoding the uri to a path
     */
    public static @NonNull String getFilePath(@NonNull Context context, @NonNull Uri uri) throws IllegalArgumentException {
        try {
            String path = internalGetFilePath(context, uri);
            if (TextUtils.isEmpty(path)) {
                throw new IllegalArgumentException("Unable to decode the given uri to a file path: " + uri);
            }
            return path;
        } catch (IllegalArgumentException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new IllegalArgumentException("Unable to decode the given uri to a file path: " + uri, ex);
        }
    }
 
    /**
     * As seen on StackOverflow https://stackoverflow.com/a/36714242/1207186
     * Try to find the file path of a document uri
     * @param context the application context
     * @param uri the Uri for which the path should be resolved
     * @return the path corresponding to the Uri as a String
     * @throws URISyntaxException
    */
    private static @Nullable String internalGetFilePath(@NonNull Context context, @NonNull Uri uri) throws URISyntaxException {
        String selection = null;
        String[] selectionArgs = null;
 
        if (DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
            if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                final String id = DocumentsContract.getDocumentId(uri);
                if (!TextUtils.isEmpty(id)) {
                    if (id.startsWith("raw:")) {
                        return id.replaceFirst("raw:", "");
                    }
                    uri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));
                }
            } else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
                if ("image".equals(type)) {
                    uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
                selection = "_id=?";
                selectionArgs = new String[]{
                        split[1]
                };
            }
        }
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = {
                    MediaStore.Images.Media.DATA
            };
            Cursor cursor = context.getContentResolver()
                    .query(uri, projection, selection, selectionArgs, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        throw new IllegalArgumentException("Unable to decode the given uri to a file path: " + uri);
    }
 
    public static void viewFile(String path, String mimeType, Context context) throws IOException {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        File file = new File(path);
 
        Uri contentUri = FileProvider.getUriForFile(context,
                context.getApplicationContext().getPackageName() + ".screenshot_provider", file);
        intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(contentUri, mimeType);
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, R.string.activity_error_no_app_for_gpx, Toast.LENGTH_LONG).show();
        }
    }
 
    public static void shareBytesAsFile(final Context context, final String name, final byte[] bytes) throws IOException {
        final File cacheDir = context.getCacheDir();
        final File rawCacheDir = new File(cacheDir, "raw");
        rawCacheDir.mkdir();
        final File file = new File(rawCacheDir, name);
 
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            outputStream.write(bytes);
        } catch (final IOException e) {
            LOG.error("Failed to write bytes to temporary file", e);
            return;
        }
 
        shareFile(context, file);
    }
 
    public static void shareFile(final Context context, final File file) throws IOException {
        if (!file.exists()) {
            LOG.warn("File {} does not exist", file.getPath());
            return;
        }
 
        shareFile(context, file, "*/*");
    }
 
    public static void shareFile(final Context context, final File file, final String type) throws IOException {
        final Uri contentUri = FileProvider.getUriForFile(
                context,
                context.getApplicationContext().getPackageName() + ".screenshot_provider",
                file
        );
 
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType(type);
        intent.putExtra(Intent.EXTRA_STREAM, contentUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            context.startActivity(Intent.createChooser(intent, "Share file"));
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, R.string.activity_error_share_failed, Toast.LENGTH_LONG).show();
        }
    }
 
    public static void openWebsite(String url){
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        GBApplication.getContext().startActivity(i);
    }
 
    public static void openApp(String packageName) throws ClassNotFoundException {
        Context context = GBApplication.getContext();
 
        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if(launchIntent == null){
            throw new ClassNotFoundException("App " + packageName + " cannot be found");
        }
        GBApplication.getContext().startActivity(launchIntent);
    }
 
    public static PowerManager.WakeLock acquirePartialWakeLock(Context context, String tag, long timeout) {
        try {
            PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = powermanager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Gadgetbridge:" + tag);
            wl.acquire(timeout);
            return wl;
        } catch (final Exception e) {
            LOG.error("Failed to take partial wake lock {}: ", tag, e);
            return null;
        }
    }
}