2021-06-20 14:17:18 +02:00
|
|
|
/* Copyright (C) 2021 Arjan Schrijver
|
|
|
|
|
|
|
|
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/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.qhybrid;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
2021-07-22 22:04:31 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
2021-06-20 14:17:18 +02:00
|
|
|
import android.content.ContentResolver;
|
2021-07-22 22:04:31 +02:00
|
|
|
import android.content.Context;
|
2021-06-20 14:17:18 +02:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
2021-07-22 22:04:31 +02:00
|
|
|
import android.content.IntentFilter;
|
2021-06-20 14:17:18 +02:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Bitmap;
|
2021-06-23 20:54:16 +02:00
|
|
|
import android.graphics.BitmapFactory;
|
2021-06-20 14:17:18 +02:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Matrix;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
import android.util.DisplayMetrics;
|
2021-07-16 11:36:46 +02:00
|
|
|
import android.view.DragEvent;
|
2021-07-11 14:37:37 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2021-06-20 14:17:18 +02:00
|
|
|
import android.view.View;
|
2021-08-08 22:47:25 +02:00
|
|
|
import android.widget.AdapterView;
|
2021-07-14 22:40:02 +02:00
|
|
|
import android.widget.ArrayAdapter;
|
2021-07-11 22:14:45 +02:00
|
|
|
import android.widget.Button;
|
2021-11-22 21:28:47 +01:00
|
|
|
import android.widget.CheckBox;
|
2021-06-20 14:17:18 +02:00
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.RelativeLayout;
|
2021-07-14 22:40:02 +02:00
|
|
|
import android.widget.Spinner;
|
2021-06-20 14:17:18 +02:00
|
|
|
import android.widget.TextView;
|
2021-07-11 22:14:45 +02:00
|
|
|
import android.widget.Toast;
|
2021-06-20 14:17:18 +02:00
|
|
|
|
2021-07-22 22:04:31 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
2021-06-23 20:54:16 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2021-06-20 14:17:18 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.File;
|
2021-06-23 20:54:16 +02:00
|
|
|
import java.io.FileInputStream;
|
2021-06-20 14:17:18 +02:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.ArrayList;
|
2021-08-08 22:47:25 +02:00
|
|
|
import java.util.Arrays;
|
2021-06-23 20:54:16 +02:00
|
|
|
import java.util.Iterator;
|
2021-07-14 22:40:02 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2021-08-08 22:47:25 +02:00
|
|
|
import java.util.TimeZone;
|
2021-06-20 14:17:18 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2021-07-11 14:04:37 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
|
2021-06-20 14:17:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2021-07-21 21:27:58 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
|
2021-07-22 22:04:31 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport;
|
2021-06-20 14:17:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2021-07-11 22:14:45 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2021-06-20 14:17:18 +02:00
|
|
|
|
2021-07-16 11:36:46 +02:00
|
|
|
public class HybridHRWatchfaceDesignerActivity extends AbstractGBActivity implements View.OnClickListener, View.OnLongClickListener, View.OnDragListener {
|
2021-06-20 14:17:18 +02:00
|
|
|
private final Logger LOG = LoggerFactory.getLogger(HybridHRWatchfaceDesignerActivity.class);
|
|
|
|
private GBDevice mGBDevice;
|
|
|
|
private DeviceCoordinator mCoordinator;
|
|
|
|
private int displayImageSize = 0;
|
|
|
|
private float scaleFactor = 0;
|
|
|
|
private ImageView backgroundImageView;
|
|
|
|
private Bitmap selectedBackgroundImage, processedBackgroundImage;
|
|
|
|
private String watchfaceName = "NewWatchface";
|
|
|
|
final private ArrayList<HybridHRWatchfaceWidget> widgets = new ArrayList<>();
|
2021-07-17 22:24:52 +02:00
|
|
|
private HybridHRWatchfaceSettings watchfaceSettings = new HybridHRWatchfaceSettings();
|
2021-07-22 13:09:00 +02:00
|
|
|
private int defaultWidgetColor = HybridHRWatchfaceWidget.COLOR_WHITE;
|
2021-07-22 22:04:31 +02:00
|
|
|
private boolean readyToCloseActivity = false;
|
|
|
|
|
2021-11-16 14:25:58 +01:00
|
|
|
private final int CHILD_ACTIVITY_IMAGE_CHOOSER = 0;
|
|
|
|
private final int CHILD_ACTIVITY_SETTINGS = 1;
|
2022-07-22 12:28:19 +02:00
|
|
|
private final int CHILD_ACTIVITY_WIDGET = 2;
|
2021-11-16 14:25:58 +01:00
|
|
|
|
2021-07-22 22:04:31 +02:00
|
|
|
BroadcastReceiver fileUploadReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (intent.getAction().equals(QHybridSupport.QHYBRID_ACTION_UPLOADED_FILE)) {
|
|
|
|
boolean success = intent.getBooleanExtra("EXTRA_SUCCESS", false);
|
|
|
|
findViewById(R.id.watchface_upload_progress_bar).setVisibility(View.GONE);
|
|
|
|
if (success) {
|
|
|
|
if (readyToCloseActivity) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
readyToCloseActivity = false;
|
|
|
|
new AlertDialog.Builder(HybridHRWatchfaceDesignerActivity.this)
|
|
|
|
.setMessage(R.string.watchface_upload_failed)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-06-20 14:17:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_hybridhr_watchface_designer);
|
|
|
|
|
|
|
|
Intent intent = getIntent();
|
|
|
|
Bundle bundle = intent.getExtras();
|
|
|
|
if (bundle != null) {
|
|
|
|
mGBDevice = bundle.getParcelable(GBDevice.EXTRA_DEVICE);
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Must provide a device when invoking this activity");
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:04:31 +02:00
|
|
|
IntentFilter intentFilter = new IntentFilter();
|
|
|
|
intentFilter.addAction(QHybridSupport.QHYBRID_ACTION_UPLOADED_FILE);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(fileUploadReceiver, intentFilter);
|
|
|
|
|
2021-06-23 20:54:16 +02:00
|
|
|
mCoordinator = DeviceHelper.getInstance().getCoordinator(mGBDevice);
|
2021-06-20 14:17:18 +02:00
|
|
|
calculateDisplayImageSize();
|
|
|
|
backgroundImageView = findViewById(R.id.hybridhr_background_image);
|
2021-06-23 20:54:16 +02:00
|
|
|
|
|
|
|
if (bundle.containsKey(GBDevice.EXTRA_UUID)) {
|
|
|
|
String appUUID = bundle.getString(GBDevice.EXTRA_UUID);
|
|
|
|
loadConfigurationFromApp(appUUID);
|
|
|
|
}
|
|
|
|
|
2021-06-20 14:17:18 +02:00
|
|
|
renderWatchfacePreview();
|
|
|
|
|
2021-07-16 11:36:46 +02:00
|
|
|
backgroundImageView.setOnDragListener(this);
|
2021-07-18 20:45:57 +02:00
|
|
|
findViewById(R.id.watchface_widget_delete_droparea).setOnDragListener(this);
|
2021-07-22 13:09:00 +02:00
|
|
|
findViewById(R.id.watchface_invert_colors).setOnClickListener(this);
|
2021-06-20 14:17:18 +02:00
|
|
|
findViewById(R.id.button_edit_name).setOnClickListener(this);
|
|
|
|
findViewById(R.id.button_set_background).setOnClickListener(this);
|
|
|
|
findViewById(R.id.button_add_widget).setOnClickListener(this);
|
|
|
|
findViewById(R.id.button_watchface_settings).setOnClickListener(this);
|
2021-10-14 11:00:37 +02:00
|
|
|
findViewById(R.id.watchface_rotate_left).setOnClickListener(this);
|
|
|
|
findViewById(R.id.watchface_rotate_right).setOnClickListener(this);
|
2021-11-17 14:28:39 +01:00
|
|
|
findViewById(R.id.watchface_remove_image).setOnClickListener(this);
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, resultData);
|
2021-11-16 14:25:58 +01:00
|
|
|
if (requestCode == CHILD_ACTIVITY_IMAGE_CHOOSER && resultCode == Activity.RESULT_OK) {
|
2021-06-20 14:17:18 +02:00
|
|
|
Uri imageUri = resultData.getData();
|
|
|
|
if (imageUri == null) {
|
|
|
|
LOG.warn("No image selected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
selectedBackgroundImage = createImageFromURI(imageUri);
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("Error converting selected image to Bitmap", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
renderWatchfacePreview();
|
2021-11-16 14:25:58 +01:00
|
|
|
} else if (requestCode == CHILD_ACTIVITY_SETTINGS && resultCode == Activity.RESULT_OK && resultData != null) {
|
|
|
|
watchfaceSettings = (HybridHRWatchfaceSettings) resultData.getSerializableExtra("watchfaceSettings");
|
2022-07-22 12:28:19 +02:00
|
|
|
} else if (requestCode == CHILD_ACTIVITY_WIDGET && resultCode == Activity.RESULT_OK && resultData != null) {
|
|
|
|
int widgetIndex = (int) resultData.getSerializableExtra("widgetIndex");
|
|
|
|
HybridHRWatchfaceWidget editedWidget = (HybridHRWatchfaceWidget) resultData.getSerializableExtra("widgetSettings");
|
|
|
|
if (widgetIndex >= 0) {
|
|
|
|
widgets.set(widgetIndex, editedWidget);
|
|
|
|
} else {
|
|
|
|
widgets.add(editedWidget);
|
|
|
|
}
|
|
|
|
renderWatchfacePreview();
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 14:37:37 +02:00
|
|
|
// Add action bar buttons
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.activity_hybridhr_watchface_designer_actionbar, menu);
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle action bar button presses
|
|
|
|
@Override
|
2021-07-11 22:14:45 +02:00
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2021-07-11 14:37:37 +02:00
|
|
|
int id = item.getItemId();
|
|
|
|
if (id == R.id.button_save_watchface) {
|
|
|
|
sendToWatch(false);
|
|
|
|
} else if (id == R.id.button_preview_watchface) {
|
|
|
|
sendToWatch(true);
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2021-06-20 14:17:18 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (v.getId() == R.id.button_edit_name) {
|
|
|
|
final EditText input = new EditText(this);
|
|
|
|
input.setText(watchfaceName);
|
|
|
|
input.setId(0);
|
|
|
|
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
|
|
|
|
LinearLayout.LayoutParams.MATCH_PARENT,
|
|
|
|
LinearLayout.LayoutParams.MATCH_PARENT);
|
|
|
|
input.setLayoutParams(lp);
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setView(input)
|
|
|
|
.setNegativeButton(R.string.fossil_hr_new_action_cancel, null)
|
|
|
|
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2021-06-23 20:54:16 +02:00
|
|
|
setWatchfaceName(input.getText().toString());
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
})
|
2021-07-16 12:12:58 +02:00
|
|
|
.setTitle(R.string.watchface_dialog_title_set_name)
|
2021-06-20 14:17:18 +02:00
|
|
|
.show();
|
2021-07-22 13:09:00 +02:00
|
|
|
} else if (v.getId() == R.id.watchface_invert_colors) {
|
|
|
|
if (selectedBackgroundImage != null) {
|
|
|
|
selectedBackgroundImage = BitmapUtil.invertBitmapColors(selectedBackgroundImage);
|
2022-06-17 22:42:58 +02:00
|
|
|
for (int i=0; i<widgets.size(); i++) {
|
|
|
|
HybridHRWatchfaceWidget widget = widgets.get(i);
|
|
|
|
widget.setColor(widget.getColor() ^ 1);
|
|
|
|
widgets.set(i, widget);
|
|
|
|
}
|
2021-07-22 13:09:00 +02:00
|
|
|
renderWatchfacePreview();
|
|
|
|
if (defaultWidgetColor == HybridHRWatchfaceWidget.COLOR_WHITE) {
|
|
|
|
defaultWidgetColor = HybridHRWatchfaceWidget.COLOR_BLACK;
|
|
|
|
} else {
|
|
|
|
defaultWidgetColor = HybridHRWatchfaceWidget.COLOR_WHITE;
|
|
|
|
}
|
|
|
|
}
|
2021-06-20 14:17:18 +02:00
|
|
|
} else if (v.getId() == R.id.button_set_background) {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
intent.setType("image/*");
|
2021-11-16 14:25:58 +01:00
|
|
|
startActivityForResult(intent, CHILD_ACTIVITY_IMAGE_CHOOSER);
|
2021-06-20 14:17:18 +02:00
|
|
|
} else if (v.getId() == R.id.button_add_widget) {
|
|
|
|
showWidgetEditPopup(-1);
|
2021-07-17 22:24:52 +02:00
|
|
|
} else if (v.getId() == R.id.button_watchface_settings) {
|
|
|
|
showWatchfaceSettingsPopup();
|
2021-11-17 14:28:39 +01:00
|
|
|
} else if (v.getId() == R.id.watchface_rotate_left) {
|
2021-10-14 11:00:37 +02:00
|
|
|
if (selectedBackgroundImage != null) {
|
|
|
|
selectedBackgroundImage = BitmapUtil.rotateImage(selectedBackgroundImage, -90);
|
|
|
|
renderWatchfacePreview();
|
|
|
|
}
|
2021-11-17 14:28:39 +01:00
|
|
|
} else if (v.getId() == R.id.watchface_rotate_right) {
|
2021-10-14 11:00:37 +02:00
|
|
|
if (selectedBackgroundImage != null) {
|
|
|
|
selectedBackgroundImage = BitmapUtil.rotateImage(selectedBackgroundImage, 90);
|
|
|
|
renderWatchfacePreview();
|
|
|
|
}
|
2021-11-17 14:28:39 +01:00
|
|
|
} else if (v.getId() == R.id.watchface_remove_image) {
|
|
|
|
deleteWatchfaceBackground();
|
|
|
|
renderWatchfacePreview();
|
2021-10-14 11:00:37 +02:00
|
|
|
}
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:36:46 +02:00
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View view) {
|
|
|
|
view.startDrag(null, new View.DragShadowBuilder(view), view, 0);
|
|
|
|
view.setVisibility(View.INVISIBLE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onDrag(View targetView, DragEvent event) {
|
|
|
|
View draggedWidget = (View) event.getLocalState();
|
|
|
|
switch (event.getAction()) {
|
2021-07-18 20:45:57 +02:00
|
|
|
case DragEvent.ACTION_DRAG_STARTED:
|
|
|
|
findViewById(R.id.watchface_widget_delete_layout).setVisibility(View.VISIBLE);
|
|
|
|
break;
|
|
|
|
case DragEvent.ACTION_DRAG_ENTERED:
|
|
|
|
if (targetView.getId() == R.id.watchface_widget_delete_droparea) {
|
|
|
|
findViewById(R.id.watchface_widget_delete_droparea).setBackgroundColor(Color.RED);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DragEvent.ACTION_DRAG_EXITED:
|
|
|
|
if (targetView.getId() == R.id.watchface_widget_delete_droparea) {
|
|
|
|
findViewById(R.id.watchface_widget_delete_droparea).setBackgroundColor(Color.TRANSPARENT);
|
|
|
|
}
|
|
|
|
break;
|
2021-07-16 11:36:46 +02:00
|
|
|
case DragEvent.ACTION_DROP:
|
2021-07-18 20:45:57 +02:00
|
|
|
if (targetView.getId() == R.id.watchface_widget_delete_droparea) {
|
|
|
|
widgets.remove(draggedWidget.getId());
|
|
|
|
renderWatchfacePreview();
|
|
|
|
} else if (targetView.getId() == R.id.hybridhr_background_image) {
|
|
|
|
int posX = (int) (event.getX() / scaleFactor);
|
|
|
|
int posY = (int) (event.getY() / scaleFactor);
|
|
|
|
widgets.get(draggedWidget.getId()).setPosX(posX);
|
|
|
|
widgets.get(draggedWidget.getId()).setPosY(posY);
|
|
|
|
renderWatchfacePreview();
|
|
|
|
}
|
2021-07-16 11:36:46 +02:00
|
|
|
break;
|
|
|
|
case DragEvent.ACTION_DRAG_ENDED:
|
|
|
|
if (!event.getResult()) {
|
|
|
|
draggedWidget.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2021-07-18 20:45:57 +02:00
|
|
|
findViewById(R.id.watchface_widget_delete_droparea).setBackgroundColor(Color.TRANSPARENT);
|
|
|
|
findViewById(R.id.watchface_widget_delete_layout).setVisibility(View.GONE);
|
2021-07-16 11:36:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:54:16 +02:00
|
|
|
private void loadConfigurationFromApp(String appUUID) {
|
|
|
|
File appCacheDir;
|
|
|
|
try {
|
|
|
|
appCacheDir = mCoordinator.getAppCacheDir();
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("Could not get external dir while trying to access app cache.", e);
|
|
|
|
return;
|
|
|
|
}
|
2022-07-29 15:53:48 +02:00
|
|
|
File backgroundFile = new File(appCacheDir, appUUID + "_bg.png");
|
|
|
|
if (!backgroundFile.exists()) {
|
|
|
|
backgroundFile = new File(appCacheDir, appUUID + ".png");
|
|
|
|
}
|
2021-06-23 20:54:16 +02:00
|
|
|
try {
|
|
|
|
Bitmap cachedBackground = BitmapFactory.decodeStream(new FileInputStream(backgroundFile));
|
|
|
|
selectedBackgroundImage = BitmapUtil.convertToGrayscale(BitmapUtil.getCircularBitmap(cachedBackground));
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("Error loading cached background image", e);
|
|
|
|
}
|
|
|
|
File cachedFile = new File(appCacheDir, appUUID + mCoordinator.getAppFileExtension());
|
|
|
|
FossilFileReader fileReader;
|
|
|
|
try {
|
|
|
|
fileReader = new FossilFileReader(Uri.fromFile(cachedFile), this);
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("Could not open cached app file", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setWatchfaceName(fileReader.getName());
|
|
|
|
JSONObject configJSON;
|
|
|
|
try {
|
|
|
|
configJSON = fileReader.getConfigJSON("customWatchFace");
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("Could not read config from cached app file", e);
|
|
|
|
return;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
LOG.warn("JSON parsing error", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (configJSON == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (Iterator<String> it = configJSON.keys(); it.hasNext(); ) {
|
|
|
|
String key = it.next();
|
|
|
|
if (key.equals("layout")) {
|
|
|
|
try {
|
|
|
|
JSONArray layout = configJSON.getJSONArray(key);
|
|
|
|
for (int i = 0; i < layout.length(); i++) {
|
|
|
|
JSONObject layoutItem = layout.getJSONObject(i);
|
|
|
|
if (layoutItem.getString("type").equals("comp")) {
|
2022-06-13 16:38:10 +02:00
|
|
|
widgets.add(HybridHRWatchfaceFactory.parseWidgetJSON(layoutItem));
|
2021-06-23 20:54:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
LOG.warn("JSON parsing error", e);
|
|
|
|
}
|
2021-07-17 22:24:52 +02:00
|
|
|
} else if (key.equals("config")) {
|
|
|
|
try {
|
|
|
|
JSONObject watchfaceConfig = configJSON.getJSONObject(key);
|
|
|
|
if (watchfaceConfig.has("timeout_display_full")) {
|
|
|
|
watchfaceSettings.setDisplayTimeoutFull(watchfaceConfig.getInt("timeout_display_full") / 60 / 1000);
|
|
|
|
}
|
|
|
|
if (watchfaceConfig.has("timeout_display_partial")) {
|
|
|
|
watchfaceSettings.setDisplayTimeoutPartial(watchfaceConfig.getInt("timeout_display_partial") / 60 / 1000);
|
|
|
|
}
|
|
|
|
if (watchfaceConfig.has("wrist_flick_hands_relative")) {
|
|
|
|
watchfaceSettings.setWristFlickHandsMoveRelative(watchfaceConfig.getBoolean("wrist_flick_hands_relative"));
|
|
|
|
}
|
|
|
|
if (watchfaceConfig.has("wrist_flick_duration")) {
|
|
|
|
watchfaceSettings.setWristFlickDuration(watchfaceConfig.getInt("wrist_flick_duration"));
|
|
|
|
}
|
|
|
|
if (watchfaceConfig.has("wrist_flick_move_hour")) {
|
|
|
|
watchfaceSettings.setWristFlickMoveHour(watchfaceConfig.getInt("wrist_flick_move_hour"));
|
|
|
|
}
|
|
|
|
if (watchfaceConfig.has("wrist_flick_move_minute")) {
|
|
|
|
watchfaceSettings.setWristFlickMoveMinute(watchfaceConfig.getInt("wrist_flick_move_minute"));
|
|
|
|
}
|
2022-07-25 22:23:22 +02:00
|
|
|
if (watchfaceConfig.has("toggle_widgets_event")) {
|
|
|
|
watchfaceSettings.setToggleWidgetsEvent(watchfaceConfig.getString("toggle_widgets_event"));
|
|
|
|
}
|
2021-08-09 14:27:38 +02:00
|
|
|
if (watchfaceConfig.has("powersave_display")) {
|
|
|
|
watchfaceSettings.setPowersaveDisplay(watchfaceConfig.getBoolean("powersave_display"));
|
|
|
|
}
|
|
|
|
if (watchfaceConfig.has("powersave_hands")) {
|
|
|
|
watchfaceSettings.setPowersaveHands(watchfaceConfig.getBoolean("powersave_hands"));
|
|
|
|
}
|
2022-03-22 20:52:43 +01:00
|
|
|
if (watchfaceConfig.has("light_up_on_notification")) {
|
|
|
|
watchfaceSettings.setLightUpOnNotification(watchfaceConfig.getBoolean("light_up_on_notification"));
|
|
|
|
}
|
2021-07-17 22:24:52 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
LOG.warn("JSON parsing error", e);
|
|
|
|
}
|
2021-06-23 20:54:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setWatchfaceName(String name) {
|
|
|
|
watchfaceName = name;
|
|
|
|
TextView watchfaceNameView = findViewById(R.id.watchface_name);
|
|
|
|
watchfaceNameView.setText(watchfaceName);
|
|
|
|
}
|
|
|
|
|
2021-06-20 14:17:18 +02:00
|
|
|
private void renderWatchfacePreview() {
|
2022-07-24 22:32:53 +02:00
|
|
|
int widgetSize = QHybridConstants.HYBRID_HR_WATCHFACE_WIDGET_SIZE;
|
2021-07-20 15:34:35 +02:00
|
|
|
if (selectedBackgroundImage == null) {
|
|
|
|
try {
|
|
|
|
selectedBackgroundImage = BitmapUtil.getCircularBitmap(BitmapFactory.decodeStream(getAssets().open("fossil_hr/default_background.png")));
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("Loading default watchface background failed", e);
|
|
|
|
}
|
|
|
|
}
|
2021-06-20 14:17:18 +02:00
|
|
|
if (selectedBackgroundImage == null) {
|
2021-11-17 14:28:39 +01:00
|
|
|
deleteWatchfaceBackground();
|
2021-06-20 14:17:18 +02:00
|
|
|
} else {
|
2021-06-23 20:54:16 +02:00
|
|
|
processedBackgroundImage = Bitmap.createScaledBitmap(selectedBackgroundImage, displayImageSize, displayImageSize, true);
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
// Remove existing widget ImageViews
|
2021-07-16 11:36:46 +02:00
|
|
|
RelativeLayout previewLayout = this.findViewById(R.id.watchface_preview_layout);
|
2021-06-20 14:17:18 +02:00
|
|
|
boolean onlyPreviewIsRemaining = false;
|
|
|
|
while (!onlyPreviewIsRemaining) {
|
2021-07-16 11:36:46 +02:00
|
|
|
int childCount = previewLayout.getChildCount();
|
2021-06-20 14:17:18 +02:00
|
|
|
int i;
|
|
|
|
for(i=0; i<childCount; i++) {
|
2021-07-16 11:36:46 +02:00
|
|
|
View currentChild = previewLayout.getChildAt(i);
|
2021-06-20 14:17:18 +02:00
|
|
|
if (currentChild.getId() != R.id.hybridhr_background_image) {
|
2021-07-16 11:36:46 +02:00
|
|
|
previewLayout.removeView(currentChild);
|
2021-06-20 14:17:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == childCount) {
|
|
|
|
onlyPreviewIsRemaining = true;
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 14:28:39 +01:00
|
|
|
// Paint a gray circle around the watchface
|
|
|
|
Canvas backgroundImageCanvas = new Canvas(processedBackgroundImage);
|
|
|
|
Paint circlePaint = new Paint();
|
|
|
|
circlePaint.setColor(Color.GRAY);
|
|
|
|
circlePaint.setAntiAlias(true);
|
|
|
|
circlePaint.setStrokeWidth(3);
|
|
|
|
circlePaint.setStyle(Paint.Style.STROKE);
|
|
|
|
backgroundImageCanvas.drawCircle(displayImageSize/2f, displayImageSize/2f, displayImageSize/2f - 2, circlePaint);
|
2021-06-20 14:17:18 +02:00
|
|
|
// Dynamically add an ImageView for each widget
|
|
|
|
Paint widgetPaint = new Paint();
|
|
|
|
widgetPaint.setColor(Color.RED);
|
|
|
|
widgetPaint.setStyle(Paint.Style.STROKE);
|
|
|
|
widgetPaint.setStrokeWidth(5);
|
2021-07-11 20:51:44 +02:00
|
|
|
Bitmap widgetNoPreviewBitmap = Bitmap.createBitmap((int)(widgetSize * scaleFactor), (int)(widgetSize * scaleFactor), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas widgetCanvas = new Canvas(widgetNoPreviewBitmap);
|
2021-06-20 14:17:18 +02:00
|
|
|
widgetCanvas.drawRect(0, 0, widgetSize * scaleFactor, widgetSize * scaleFactor, widgetPaint);
|
|
|
|
for (int i=0; i<widgets.size(); i++) {
|
|
|
|
HybridHRWatchfaceWidget widget = widgets.get(i);
|
|
|
|
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
|
|
|
|
layoutParams.addRule(RelativeLayout.ALIGN_LEFT, backgroundImageView.getId());
|
|
|
|
layoutParams.addRule(RelativeLayout.ALIGN_TOP, backgroundImageView.getId());
|
|
|
|
layoutParams.setMargins((int) ((widget.getPosX() - widgetSize/2) * scaleFactor), (int) ((widget.getPosY() - widgetSize/2) * scaleFactor), 0, 0);
|
|
|
|
ImageView widgetView = new ImageView(this);
|
|
|
|
widgetView.setId(i);
|
2021-07-11 20:51:44 +02:00
|
|
|
try {
|
|
|
|
widgetView.setImageBitmap(Bitmap.createScaledBitmap(widget.getPreviewImage(this), (int)(widgetSize * scaleFactor), (int)(widgetSize * scaleFactor), true));
|
|
|
|
} catch (IOException e) {
|
|
|
|
widgetView.setImageBitmap(widgetNoPreviewBitmap);
|
|
|
|
}
|
2021-06-20 14:17:18 +02:00
|
|
|
widgetView.setLayoutParams(layoutParams);
|
|
|
|
widgetView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
showWidgetEditPopup(v.getId());
|
|
|
|
}
|
|
|
|
});
|
2021-07-16 11:36:46 +02:00
|
|
|
widgetView.setOnLongClickListener(this);
|
|
|
|
previewLayout.addView(widgetView);
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
backgroundImageView.setImageBitmap(processedBackgroundImage);
|
|
|
|
}
|
|
|
|
|
2022-07-22 12:28:19 +02:00
|
|
|
private void showWidgetEditPopup(int index) {
|
|
|
|
HybridHRWatchfaceWidget widget;
|
2021-06-20 14:17:18 +02:00
|
|
|
if (index >= 0) {
|
|
|
|
widget = widgets.get(index);
|
2021-07-22 13:09:00 +02:00
|
|
|
} else {
|
2022-07-22 12:28:19 +02:00
|
|
|
int posX = 120;
|
|
|
|
int posY = 120;
|
2022-03-09 10:44:49 +01:00
|
|
|
int currentIndex = widgets.size();
|
2022-07-22 12:28:19 +02:00
|
|
|
if (currentIndex < 4) {
|
|
|
|
HybridHRWidgetPosition newPosition = HybridHRWatchfaceWidget.defaultPositions[currentIndex];
|
|
|
|
posX = newPosition.posX;
|
|
|
|
posY = newPosition.posY;
|
2022-03-09 10:44:49 +01:00
|
|
|
GB.toast(getString(R.string.watchface_dialog_pre_setting_position, getString(newPosition.hintStringResource)), Toast.LENGTH_SHORT, GB.INFO);
|
2021-07-11 22:14:45 +02:00
|
|
|
}
|
2022-07-24 22:32:53 +02:00
|
|
|
int color = defaultWidgetColor;
|
2022-07-22 12:28:19 +02:00
|
|
|
if (widgets.size() > 0) {
|
|
|
|
color = widgets.get(0).getColor();
|
2021-08-08 22:47:25 +02:00
|
|
|
}
|
2022-07-22 12:28:19 +02:00
|
|
|
widget = new HybridHRWatchfaceWidget("widgetDate", posX, posY, 76, 76, color, null);
|
|
|
|
}
|
|
|
|
Intent intent = new Intent(this, HybridHRWatchfaceWidgetActivity.class);
|
|
|
|
intent.putExtra("widgetIndex", index);
|
|
|
|
intent.putExtra("widgetSettings", widget);
|
|
|
|
startActivityForResult(intent, CHILD_ACTIVITY_WIDGET);
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
|
2021-07-17 22:24:52 +02:00
|
|
|
private void showWatchfaceSettingsPopup() {
|
2021-11-16 14:25:58 +01:00
|
|
|
Intent intent = new Intent(this, HybridHRWatchfaceSettingsActivity.class);
|
|
|
|
intent.putExtra("watchfaceSettings", watchfaceSettings);
|
|
|
|
startActivityForResult(intent, CHILD_ACTIVITY_SETTINGS);
|
2021-07-17 22:24:52 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 14:17:18 +02:00
|
|
|
private void calculateDisplayImageSize() {
|
|
|
|
DisplayMetrics displayMetrics = new DisplayMetrics();
|
|
|
|
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
|
|
|
displayImageSize = (int) Math.round(displayMetrics.widthPixels * 0.75);
|
|
|
|
scaleFactor = displayImageSize / 240f;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap createImageFromURI(Uri imageUri) throws IOException, RuntimeException {
|
|
|
|
if (imageUri == null) {
|
|
|
|
throw new RuntimeException("No image selected");
|
|
|
|
}
|
|
|
|
|
|
|
|
// UriHelper uriHelper = UriHelper.get(imageUri, this);
|
|
|
|
// InputStream in = new BufferedInputStream(uriHelper.openInputStream());
|
|
|
|
// Bitmap bitmap = BitmapFactory.decodeStream(in);
|
|
|
|
|
|
|
|
ContentResolver resolver = getContentResolver();
|
|
|
|
Cursor c = resolver.query(imageUri, new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);
|
|
|
|
c.moveToFirst();
|
|
|
|
int orientation = c.getInt(c.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
|
|
|
|
c.close();
|
|
|
|
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
|
|
|
|
if (orientation != 0) { // FIXME: doesn't seem to work
|
|
|
|
Matrix matrix = new Matrix();
|
|
|
|
matrix.postRotate(90);
|
|
|
|
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return BitmapUtil.convertToGrayscale(BitmapUtil.getCircularBitmap(bitmap));
|
|
|
|
}
|
|
|
|
|
2021-11-17 14:28:39 +01:00
|
|
|
private void deleteWatchfaceBackground() {
|
|
|
|
selectedBackgroundImage = Bitmap.createBitmap(displayImageSize, displayImageSize, Bitmap.Config.ARGB_8888);
|
|
|
|
selectedBackgroundImage.eraseColor(Color.BLACK);
|
|
|
|
selectedBackgroundImage = BitmapUtil.getCircularBitmap(selectedBackgroundImage);
|
|
|
|
}
|
|
|
|
|
2021-06-20 14:17:18 +02:00
|
|
|
private void sendToWatch(boolean preview) {
|
2022-07-29 15:53:48 +02:00
|
|
|
final Context mContext = this;
|
|
|
|
final HybridHRWatchfaceFactory wfFactory;
|
2021-06-20 14:17:18 +02:00
|
|
|
if (preview) {
|
|
|
|
wfFactory = new HybridHRWatchfaceFactory("previewWatchface");
|
|
|
|
} else {
|
|
|
|
wfFactory = new HybridHRWatchfaceFactory(watchfaceName);
|
|
|
|
}
|
2021-07-17 22:24:52 +02:00
|
|
|
wfFactory.setSettings(watchfaceSettings);
|
2022-07-31 22:53:32 +02:00
|
|
|
wfFactory.setBackground(selectedBackgroundImage);
|
2021-06-20 14:17:18 +02:00
|
|
|
wfFactory.addWidgets(widgets);
|
|
|
|
try {
|
|
|
|
File tempFile = File.createTempFile("tmpWatchfaceFile", null);
|
|
|
|
tempFile.deleteOnExit();
|
|
|
|
FileOutputStream fos = new FileOutputStream(tempFile);
|
|
|
|
BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
|
|
bos.write(wfFactory.getWapp(this));
|
|
|
|
bos.close();
|
|
|
|
fos.close();
|
2021-07-21 21:27:58 +02:00
|
|
|
final Uri tempAppFileUri = Uri.fromFile(tempFile);
|
2021-06-20 14:17:18 +02:00
|
|
|
if (preview) {
|
2021-07-22 22:04:31 +02:00
|
|
|
findViewById(R.id.watchface_upload_progress_bar).setVisibility(View.VISIBLE);
|
|
|
|
GBApplication.deviceService().onInstallApp(tempAppFileUri);
|
2021-06-20 14:17:18 +02:00
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
GBApplication.deviceService().onAppDelete(UUID.nameUUIDFromBytes("previewWatchface".getBytes(StandardCharsets.UTF_8)));
|
|
|
|
}
|
2022-07-22 12:28:19 +02:00
|
|
|
}, 15000);
|
2021-06-20 14:17:18 +02:00
|
|
|
} else {
|
2021-07-22 22:04:31 +02:00
|
|
|
readyToCloseActivity = true;
|
2021-07-21 21:27:58 +02:00
|
|
|
final FossilFileReader fossilFile = new FossilFileReader(tempAppFileUri, this);
|
|
|
|
GBDeviceApp app = fossilFile.getGBDeviceApp();
|
|
|
|
File cacheDir = mCoordinator.getAppCacheDir();
|
|
|
|
File destFile = new File(cacheDir, app.getUUID().toString() + mCoordinator.getAppFileExtension());
|
|
|
|
if (destFile.exists()) {
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setMessage(R.string.watchface_cache_confirm_overwrite)
|
|
|
|
.setNegativeButton(R.string.no, null)
|
|
|
|
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2021-07-22 22:04:31 +02:00
|
|
|
findViewById(R.id.watchface_upload_progress_bar).setVisibility(View.VISIBLE);
|
2021-07-21 21:27:58 +02:00
|
|
|
GBApplication.deviceService().onInstallApp(tempAppFileUri);
|
2022-07-31 22:53:32 +02:00
|
|
|
FossilHRInstallHandler.saveAppInCache(fossilFile, selectedBackgroundImage, wfFactory.getPreviewImage(mContext), mCoordinator, HybridHRWatchfaceDesignerActivity.this);
|
2021-07-21 21:27:58 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
} else {
|
2021-07-22 22:04:31 +02:00
|
|
|
findViewById(R.id.watchface_upload_progress_bar).setVisibility(View.VISIBLE);
|
2021-07-21 21:27:58 +02:00
|
|
|
GBApplication.deviceService().onInstallApp(tempAppFileUri);
|
2022-07-31 22:53:32 +02:00
|
|
|
FossilHRInstallHandler.saveAppInCache(fossilFile, selectedBackgroundImage, wfFactory.getPreviewImage(mContext), mCoordinator, HybridHRWatchfaceDesignerActivity.this);
|
2021-07-21 21:27:58 +02:00
|
|
|
}
|
2021-06-20 14:17:18 +02:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("Error while creating and uploading watchface", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|