2017-09-12 12:39:15 +02:00
|
|
|
/* Copyright (C) 2016-2017 Andreas Shimokawa, Carsten Pfeiffer
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>. */
|
2016-11-27 02:41:52 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
2017-07-31 22:49:05 +02:00
|
|
|
import android.app.Activity;
|
2017-01-26 00:11:52 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2017-10-19 21:52:38 +02:00
|
|
|
import android.content.Intent;
|
2017-07-31 22:49:05 +02:00
|
|
|
import android.content.res.Configuration;
|
2017-08-14 21:35:34 +02:00
|
|
|
import android.graphics.Color;
|
2017-10-19 21:52:38 +02:00
|
|
|
import android.net.Uri;
|
2016-11-27 02:41:52 +01:00
|
|
|
import android.os.ParcelUuid;
|
|
|
|
import android.os.Parcelable;
|
2017-10-19 21:52:38 +02:00
|
|
|
import android.support.v4.content.FileProvider;
|
2017-01-26 00:11:52 +01:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2016-11-27 02:41:52 +01:00
|
|
|
|
2017-10-19 21:52:38 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2017-07-31 22:49:05 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2017-08-14 21:35:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.ActivitySummariesActivity;
|
2017-08-14 21:35:34 +02:00
|
|
|
|
2016-11-27 02:41:52 +01:00
|
|
|
public class AndroidUtils {
|
2017-10-19 21:52:38 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-11-27 02:41:52 +01:00
|
|
|
public static ParcelUuid[] toParcelUUids(Parcelable[] uuids) {
|
|
|
|
if (uuids == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
ParcelUuid[] uuids2 = new ParcelUuid[uuids.length];
|
2017-10-19 21:52:38 +02:00
|
|
|
//noinspection SuspiciousSystemArraycopy
|
2016-11-27 02:41:52 +01:00
|
|
|
System.arraycopy(uuids, 0, uuids2, 0, uuids.length);
|
|
|
|
return uuids2;
|
|
|
|
}
|
2017-01-26 00:11:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 22:49:05 +02:00
|
|
|
|
2017-09-03 01:02:31 +02:00
|
|
|
public static void setLanguage(Context context, Locale language) {
|
2017-07-31 22:49:05 +02:00
|
|
|
Configuration config = new Configuration();
|
|
|
|
config.setLocale(language);
|
|
|
|
|
|
|
|
// FIXME: I have no idea what I am doing
|
2017-09-03 01:02:31 +02:00
|
|
|
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
|
2017-07-31 22:49:05 +02:00
|
|
|
}
|
2017-08-14 21:35:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 (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 (GBApplication.isDarkThemeEnabled()) {
|
|
|
|
color = context.getResources().getColor(R.color.cardview_dark_background);
|
|
|
|
} else {
|
|
|
|
color = context.getResources().getColor(R.color.cardview_light_background);
|
|
|
|
}
|
|
|
|
return colorToHex(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String colorToHex(int color) {
|
|
|
|
return "#"
|
|
|
|
+ Integer.toHexString(Color.red(color))
|
|
|
|
+ Integer.toHexString(Color.green(color))
|
|
|
|
+ Integer.toHexString(Color.blue(color));
|
|
|
|
}
|
2017-10-19 21:52:38 +02:00
|
|
|
|
|
|
|
public static void viewFile(String path, String action, Context context) throws IOException {
|
|
|
|
Intent intent = new Intent(action);
|
|
|
|
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.setData(contentUri);
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
2016-11-27 02:41:52 +01:00
|
|
|
}
|