Skip to content

Commit

Permalink
splash screen, online languages
Browse files Browse the repository at this point in the history
  • Loading branch information
francescotescari committed Jul 19, 2020
1 parent 89524f4 commit 9210ce7
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 41 deletions.
47 changes: 46 additions & 1 deletion src/com/xiaomitool/v2/engine/ToolManager.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.xiaomitool.v2.engine;

import com.xiaomitool.v2.engine.actions.ActionsStatic;
import com.xiaomitool.v2.gui.GuiUtils;
import com.xiaomitool.v2.gui.SplashScreen;
import com.xiaomitool.v2.gui.WindowManager;
import com.xiaomitool.v2.gui.controller.LoginController;
import com.xiaomitool.v2.gui.drawable.DrawableManager;
import com.xiaomitool.v2.language.Lang;
import com.xiaomitool.v2.logging.Log;
import com.xiaomitool.v2.logging.feedback.LiveFeedbackEasy;
import com.xiaomitool.v2.logging.feedback.LogSender;
import com.xiaomitool.v2.resources.ResourcesConst;
import com.xiaomitool.v2.resources.ResourcesManager;
import com.xiaomitool.v2.utility.RunnableMessage;
import com.xiaomitool.v2.utility.utils.MutexUtils;
import com.xiaomitool.v2.utility.utils.SettingsUtils;
import com.xiaomitool.v2.utility.utils.StrUtils;
import com.xiaomitool.v2.utility.utils.UpdateUtils;
import com.xiaomitool.v2.xiaomi.XiaomiKeystore;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.json.JSONException;
Expand All @@ -32,6 +38,18 @@ public class ToolManager {
private static boolean exiting = false;
private static List<Stage> activeStages = new ArrayList<>();
private static String runningInstanceId = null;
private static RunnableMessage ON_BEFORE_CLOSE = null;
private static RunnableMessage ASK_FEEDBACK = new RunnableMessage() {
@Override
public int run() throws InterruptedException {
ToolManager.setOnExitAskForFeedback(false);
if (LogSender.isLogCooldown()) {
return 0;
}
ActionsStatic.ASK_FOR_FEEDBACK().run();
return 0;
}
};

public static String getFeedbackUrl() {
return XMT_HOST + "/feedback";
Expand All @@ -42,6 +60,9 @@ public static void init(Stage primaryStage, String[] args) throws Exception {
System.exit(0);
return;
}
final SplashScreen splashScreen = new SplashScreen(WindowManager.DEFAULT_TITLE, DrawableManager.getResourceImage("splash.png"));
WindowManager.runNowOrLater(() -> splashScreen.start(new Stage()));

if (!ResourcesManager.init()) {
Log.error("Failed to init resources dir");
}
Expand All @@ -52,19 +73,37 @@ public static void init(Stage primaryStage, String[] args) throws Exception {
return;
}
SettingsUtils.load();
ActionsStatic.LOAD_ONLINE_LANGS(XMT_HOST).run();
Lang.loadSystemLanguage();
GuiUtils.init();
checkLoadSession();
//WindowManager.runNowOrLater(splashScreen::stopSplash);
Log.info("Starting XiaoMiTool V2 " + TOOL_VERSION + " : " + ResourcesConst.getLogString());
WindowManager.launchMain(primaryStage);
WindowManager.runNowOrLater(() -> WindowManager.launchMain(primaryStage, new RunnableMessage() {
@Override
public int run() throws InterruptedException {
ActionsStatic.CLOSING().run();
RunnableMessage r = ON_BEFORE_CLOSE;
if (r != null) {
return r.run();
}
return 0;
}
}));

}

public static void showStage(Stage stage) {
if (stage == null) {
return;
}
activeStages.add(stage);
SplashScreen splashScreen = SplashScreen.getInstance();
if (splashScreen != null){
splashScreen.stopSplash();
}
stage.show();
stage.requestFocus();
}

public static void closeStage(Stage stage) {
Expand Down Expand Up @@ -160,4 +199,10 @@ public static String getRunningInstanceId() {
}
return runningInstanceId;
}

public static void setOnExitAskForFeedback(boolean ask) {
ON_BEFORE_CLOSE = ask ? ASK_FEEDBACK : null;
}


}
17 changes: 17 additions & 0 deletions src/com/xiaomitool/v2/engine/actions/ActionsStatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import com.xiaomitool.v2.gui.drawable.DrawableManager;
import com.xiaomitool.v2.gui.raw.RawManager;
import com.xiaomitool.v2.gui.visual.*;
import com.xiaomitool.v2.inet.CustomHttpException;
import com.xiaomitool.v2.language.LRes;
import com.xiaomitool.v2.language.Lang;
import com.xiaomitool.v2.logging.Log;
import com.xiaomitool.v2.logging.feedback.LiveFeedbackEasy;
import com.xiaomitool.v2.procedure.install.GenericInstall;
Expand Down Expand Up @@ -520,4 +522,19 @@ public int run() throws InterruptedException {
}
};
}

public static RunnableMessage LOAD_ONLINE_LANGS(String host){
return new RunnableMessage() {
@Override
public int run() throws InterruptedException {
try {
Lang.initOnlineLangs(host);
} catch (Exception e) {
Log.error("Failed to load online languages");
Log.exc(e);
}
return 0;
}
};
}
}
13 changes: 12 additions & 1 deletion src/com/xiaomitool/v2/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ public static void main(String[] args) {

@Override
public void start(Stage primaryStage) throws Exception {
ToolManager.init(primaryStage, arguments);
new Thread(new Runnable() {
@Override
public void run() {
try {
ToolManager.init(primaryStage, arguments);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}).start();

}
}
65 changes: 65 additions & 0 deletions src/com/xiaomitool/v2/gui/SplashScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.xiaomitool.v2.gui;

import com.xiaomitool.v2.logging.Log;
import javafx.application.Application;
import javafx.application.Preloader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class SplashScreen extends Preloader {
private static SplashScreen instance;
public static SplashScreen getInstance(){
return instance;
}
private String title;
private Image image;
private Stage primaryStage, secondaryStage;

public SplashScreen(String title, Image image){
Log.debug(image);
this.title = title;
this.image = image;
}

@Override
public void start(Stage primaryStage) {
Log.debug(this.image.getHeight());
StackPane root = new StackPane(new ImageView(this.image));
Scene scene = new Scene(root);
//scene.setFill(Color.TRANSPARENT);
primaryStage.setTitle(this.title);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setOpacity(0);

Stage secondaryStage = new Stage(StageStyle.UNDECORATED);
secondaryStage.initOwner(primaryStage);
secondaryStage.setScene(scene);
primaryStage.show();
secondaryStage.show();
this.primaryStage = primaryStage;
this.secondaryStage = secondaryStage;
instance = this;

}

public void stopSplash() {
this.secondaryStage.close();
this.primaryStage.close();
instance = null;
}


@Override
public void handleStateChangeNotification(StateChangeNotification evt) {
Log.debug(evt);
if (evt.getType() == StateChangeNotification.Type.BEFORE_START) {
this.primaryStage.hide();
}
}
}
45 changes: 13 additions & 32 deletions src/com/xiaomitool/v2/gui/WindowManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,15 @@ public class WindowManager {
public static final double PREF_WIN_WIDTH = 860;
public static final double PREF_WIN_HEIGHT = 860;
private static final String FRAME_POPUP = "Popup";
private static final String DEFAULT_TITLE = "XiaoMiTool V2";
public static final String DEFAULT_TITLE = "XiaoMiTool V2";
private static final Image ICON_IMAGE = DrawableManager.getResourceImage("icon.png");
private static final DropShadow windowDropShadow = new DropShadow(10, 1, 1, Color.gray(0.7));
private static Stage mainStage;
private static ToastPane toastPane;
private static OverlayPane mainOverlay;
private static VisiblePane mainVisiblePane;
private static RunnableMessage ON_BEFORE_CLOSE = null;
private static RunnableMessage ASK_FEEDBACK = new RunnableMessage() {
@Override
public int run() throws InterruptedException {
WindowManager.setOnExitAskForFeedback(false);
if (LogSender.isLogCooldown()) {
return 0;
}
ActionsStatic.ASK_FOR_FEEDBACK().run();
return 0;
}
};


private static MainWindowController mainWindowController = null;

public static void setMainVisiblePane(VisiblePane pane) {
Expand Down Expand Up @@ -159,25 +149,13 @@ public static Stage launchLogin() {
return launchWindow(FRAME_LOGIN, new LoginController());
}

public static void setOnExitAskForFeedback(boolean ask) {
ON_BEFORE_CLOSE = ask ? ASK_FEEDBACK : null;
}

public static void launchMain(Stage primaryStage) {

public static void launchMain(Stage primaryStage, RunnableMessage onBeforeClose) {
mainStage = primaryStage;
mainWindowController = new MainWindowController();
launchWindow(FRAME_MAIN, mainWindowController, primaryStage);
mainWindowController.setOnBeforeClose(new RunnableMessage() {
@Override
public int run() throws InterruptedException {
ActionsStatic.CLOSING().run();
RunnableMessage r = ON_BEFORE_CLOSE;
if (r != null) {
return r.run();
}
return 0;
}
});
mainWindowController.setOnBeforeClose(onBeforeClose);
primaryStage.setOnCloseRequest(event -> {
ToolManager.exit(0);
});
Expand All @@ -200,11 +178,11 @@ public static Stage launchPopup(PopupWindow popupWindow) {
Runnable runnable = new Runnable() {
@Override
public void run() {
Pointer pointer = new Pointer();
Pointer<Pane> pointer = new Pointer<>();
PopupController controller = new PopupController(popupWindow);
Stage stage = launchWindow(FRAME_POPUP, controller, null, pointer);
Parent p = stage.getScene().getRoot();
((Pane) pointer.pointed).setMinSize(popupWindow.getWidth(), popupWindow.getHeight());
pointer.pointed.setMinSize(popupWindow.getWidth(), popupWindow.getHeight());
stage.setTitle(DEFAULT_TITLE);
stage.sizeToScene();
centerStage(stage);
Expand All @@ -230,7 +208,7 @@ private static void centerStage(Stage stage) {
stage.setY(y);
}

private static Stage launchWindow(String fxml, DefaultController controller, Stage primaryStage, Pointer pointer) {
private static Stage launchWindow(String fxml, DefaultController controller, Stage primaryStage, Pointer<Pane> pointer) {
boolean isMain = primaryStage == mainStage && primaryStage != null;
if (isMain) {
Log.info("Launching main window");
Expand All @@ -250,7 +228,7 @@ private static Stage launchWindow(String fxml, DefaultController controller, Sta
}
Background background = DEFAULT_BACKGROUND;
if (pointer != null) {
pointer.pointed = root;
pointer.pointed = (Pane) root;
}
if (root instanceof Pane) {
((Pane) root).setBackground(background);
Expand Down Expand Up @@ -286,6 +264,9 @@ private static Stage launchWindow(String fxml, DefaultController controller, Sta
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
if (isMain){

}
ToolManager.showStage(primaryStage);
if (!isMain) {
centerStage(primaryStage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void run() {
if (!LogSender.uploadFeedback(text, sendLogFile)) {
throw new Exception("Failed to uplaod the feedback, check the log file");
}
WindowManager.setOnExitAskForFeedback(false);
ToolManager.setOnExitAskForFeedback(false);
feedbackPopup.getController().closeWindow();
Platform.runLater(() -> settingsToast.toast(LRes.FEEDBACK_SENT.toString()));
} catch (Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions src/com/xiaomitool/v2/gui/drawable/DrawableManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public class DrawableManager {
public static final String ERROR = "error.png";

public static URL getResource(String name) {
URL resourcePath = DrawableManager.class.getResource(name);
return resourcePath;
return DrawableManager.class.getResource(name);
}

public static URL getPng(String name) {
Expand All @@ -26,6 +25,7 @@ public static URL getPng(String name) {

public static Image getResourceImage(String name) {
URL url = getResource(name);

return url == null ? null : new Image(url.toString());
}
}
Binary file added src/com/xiaomitool/v2/gui/drawable/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/com/xiaomitool/v2/gui/visual/InstallPane.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xiaomitool.v2.gui.visual;

import com.xiaomitool.v2.engine.ToolManager;
import com.xiaomitool.v2.gui.GuiUtils;
import com.xiaomitool.v2.gui.WindowManager;
import com.xiaomitool.v2.language.LRes;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void toast(String message) {

@Override
public void onException(InstallException exception) {
WindowManager.setOnExitAskForFeedback(false); //TOO MANY FEEDBACKS
ToolManager.setOnExitAskForFeedback(false); //TOO MANY FEEDBACKS
Log.log("FATAL", exception.toString(), true);
Log.exc(exception);
Log.exc(new Exception("TraceBackException"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static RInstall installationSuccess() {
return RNode.sequence(RebootDevice.rebootNoWaitIfConnected(), new RInstall() {
@Override
public void run(ProcedureRunner runner) throws InstallException, InterruptedException {
WindowManager.setOnExitAskForFeedback(false);
ToolManager.setOnExitAskForFeedback(false);
Installable installable = (Installable) runner.getContext(Procedures.INSTALLABLE);
LiveFeedbackEasy.sendSuccess(String.valueOf(installable), runner.getStackStrace());
Log.info("Installation succesful, showing donation message");
Expand Down
4 changes: 2 additions & 2 deletions src/com/xiaomitool/v2/utility/Pointer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.xiaomitool.v2.utility;

public class Pointer {
public Object pointed;
public class Pointer<T> {
public T pointed;
}

0 comments on commit 9210ce7

Please sign in to comment.