Hoky
3 years ago
41 changed files with 752 additions and 612 deletions
@ -0,0 +1,184 @@
|
||||
package com.fr.design.deeplink; |
||||
|
||||
import com.fr.design.constants.DesignerLaunchStatus; |
||||
import com.fr.design.startup.FineStartupNotificationFactory; |
||||
import com.fr.design.startup.FineStartupNotificationProvider; |
||||
import com.fr.event.Event; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.event.Listener; |
||||
import com.fr.event.Null; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.os.OperatingSystem; |
||||
import com.fr.third.org.apache.http.NameValuePair; |
||||
import com.fr.web.URLUtils; |
||||
|
||||
import javax.swing.SwingUtilities; |
||||
import java.awt.Color; |
||||
import java.awt.Frame; |
||||
import java.awt.event.WindowAdapter; |
||||
import java.awt.event.WindowEvent; |
||||
import java.io.IOException; |
||||
import java.net.MalformedURLException; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
import java.net.URLStreamHandler; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Starryi |
||||
* @version 1.0 |
||||
* Created by Starryi on 2022/1/6 |
||||
*/ |
||||
public class DeepLinkCore { |
||||
|
||||
protected DeepLinkCore(){} |
||||
private static final DeepLinkCore instance = new DeepLinkCore(); |
||||
public static DeepLinkCore getInstance(){ |
||||
return instance; |
||||
} |
||||
|
||||
private String pendingURL; |
||||
|
||||
private final List<DeepLink> deepLinkList = new ArrayList<>(); |
||||
|
||||
private boolean isDesignerStartupCompleted = false; |
||||
|
||||
public void register(DeepLink deepLink) { |
||||
if (deepLink != null) { |
||||
deepLinkList.add(deepLink); |
||||
} |
||||
} |
||||
|
||||
public void start(String[] args) { |
||||
if (OperatingSystem.isWindows()) { |
||||
if (args.length > 0) { |
||||
receiveDeeplink(args[0]); |
||||
} |
||||
} |
||||
|
||||
if (OperatingSystem.isWindows() && args.length > 0) { |
||||
receiveDeeplink(args[0]); |
||||
} |
||||
|
||||
FineStartupNotificationFactory.getNotification() |
||||
.registerStartupListener(new FineStartupNotificationProvider.Listener() { |
||||
@Override |
||||
public void startupPerformed(String parameters) { |
||||
receiveDeeplink(parameters); |
||||
} |
||||
}); |
||||
|
||||
EventDispatcher.listen(DesignerLaunchStatus.STARTUP_COMPLETE, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
EventDispatcher.stopListen(this); |
||||
isDesignerStartupCompleted = true; |
||||
if (canConsumePendingURL()) { |
||||
consumePendingURL(); |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public void receiveDeeplink(String url) { |
||||
if (canAcceptNewURL()) { |
||||
acceptNewURL(url); |
||||
if (canConsumePendingURL()) { |
||||
consumePendingURL(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void receiveDeeplink2(String url) { |
||||
if (canAcceptNewURL()) { |
||||
acceptNewURL(url); |
||||
if (canConsumePendingURL()) { |
||||
consumePendingURL(); |
||||
} else { |
||||
Frame frame = new Frame("can not ConsumePendingURL"); |
||||
frame.setSize(400, 400); |
||||
frame.setBackground(Color.BLACK); |
||||
frame.addWindowListener(new WindowAdapter() { |
||||
public void windowClosing(WindowEvent windowEvent){ |
||||
frame.dispose(); |
||||
} |
||||
}); |
||||
frame.setVisible(true); |
||||
} |
||||
} else { |
||||
Frame frame = new Frame("can not AcceptNewURL"); |
||||
frame.setSize(400, 400); |
||||
frame.setBackground(Color.BLACK); |
||||
frame.addWindowListener(new WindowAdapter() { |
||||
public void windowClosing(WindowEvent windowEvent){ |
||||
frame.dispose(); |
||||
} |
||||
}); |
||||
frame.setVisible(true); |
||||
} |
||||
} |
||||
|
||||
private boolean canAcceptNewURL() { |
||||
return StringUtils.isEmpty(this.pendingURL); |
||||
} |
||||
|
||||
private void acceptNewURL(String url) { |
||||
this.pendingURL = url; |
||||
} |
||||
|
||||
private boolean canConsumePendingURL() { |
||||
return StringUtils.isNotEmpty(this.pendingURL) && isDesignerStartupCompleted; |
||||
} |
||||
|
||||
private void consumePendingURL() { |
||||
String host = null; |
||||
String path = null; |
||||
Map<String, Object> params = new HashMap<>(); |
||||
|
||||
URL url = null; |
||||
try { |
||||
url = new URL(null, this.pendingURL, new URLStreamHandler() { |
||||
@Override |
||||
protected URLConnection openConnection(URL u) throws IOException { |
||||
return null; |
||||
} |
||||
}); |
||||
} catch (MalformedURLException ignored) {} |
||||
|
||||
if (url != null) { |
||||
host = url.getHost(); |
||||
path = url.getPath(); |
||||
|
||||
List<NameValuePair> pairs = URLUtils.parse(url.getQuery()); |
||||
for (NameValuePair pair: pairs) { |
||||
params.put(pair.getName(), pair.getValue()); |
||||
} |
||||
} |
||||
|
||||
FineLoggerFactory.getLogger().info("consume deep link: " + this.pendingURL); |
||||
performDeepLinks(this.pendingURL, host, path, params); |
||||
|
||||
markPendingURLConsumed(); |
||||
} |
||||
|
||||
private void performDeepLinks(String url, String host, String path, Map<String, Object> params) { |
||||
SwingUtilities.invokeLater(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
for (DeepLink deepLink: deepLinkList) { |
||||
if (deepLink.accept(url, host, path, params)) { |
||||
deepLink.run(url, host, path, params); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
private void markPendingURLConsumed() { |
||||
this.pendingURL = null; |
||||
} |
||||
} |
@ -1,153 +1,21 @@
|
||||
package com.fr.design.deeplink; |
||||
|
||||
import com.fr.design.constants.DesignerLaunchStatus; |
||||
import com.fr.design.startup.FineStartupNotificationFactory; |
||||
import com.fr.design.startup.FineStartupNotificationProvider; |
||||
import com.fr.event.Event; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.event.Listener; |
||||
import com.fr.event.Null; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.os.OperatingSystem; |
||||
import com.fr.third.org.apache.http.NameValuePair; |
||||
import com.fr.web.URLUtils; |
||||
|
||||
import javax.swing.SwingUtilities; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.net.MalformedURLException; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
import java.net.URLStreamHandler; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author Starryi |
||||
* @version 1.0 |
||||
* Created by Starryi on 2022/1/6 |
||||
* Created by Starryi on 2022/2/10 |
||||
*/ |
||||
public class DeepLinkManager { |
||||
|
||||
private DeepLinkManager(){} |
||||
protected DeepLinkManager(){} |
||||
private static final DeepLinkManager instance = new DeepLinkManager(); |
||||
public static DeepLinkManager getInstance(){ |
||||
return instance; |
||||
} |
||||
|
||||
private String pendingURL; |
||||
|
||||
private final List<DeepLink> deepLinkList = new ArrayList<>(); |
||||
|
||||
private boolean isDesignerStartUpCompleted = false; |
||||
|
||||
private void register(DeepLink deepLink) { |
||||
if (deepLink != null) { |
||||
deepLinkList.add(deepLink); |
||||
} |
||||
} |
||||
|
||||
public void prepare() { |
||||
register(new FileOpen4MacDeepLink()); |
||||
register(new TemplateThemeInstallationDeepLink()); |
||||
|
||||
FineStartupNotificationFactory.getNotification() |
||||
.registerStartupListener(new FineStartupNotificationProvider.Listener() { |
||||
@Override |
||||
public void startupPerformed(String parameters) { |
||||
if (canAcceptNewURL()) { |
||||
acceptNewURL(parameters); |
||||
if (canConsumePendingURL()) { |
||||
consumePendingURL(); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
|
||||
EventDispatcher.listen(DesignerLaunchStatus.STARTUP_COMPLETE, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
isDesignerStartUpCompleted = true; |
||||
if (canConsumePendingURL()) { |
||||
consumePendingURL(); |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
private boolean canAcceptNewURL() { |
||||
return StringUtils.isEmpty(this.pendingURL); |
||||
} |
||||
|
||||
private void acceptNewURL(String url) { |
||||
this.pendingURL = url; |
||||
} |
||||
|
||||
private boolean canConsumePendingURL() { |
||||
return StringUtils.isNotEmpty(this.pendingURL) && isDesignerStartUpCompleted; |
||||
} |
||||
|
||||
private void consumePendingURL() { |
||||
String host = null; |
||||
String path = null; |
||||
Map<String, Object> params = new HashMap<>(); |
||||
|
||||
URL url = null; |
||||
try { |
||||
url = new URL(null, this.pendingURL, new URLStreamHandler() { |
||||
@Override |
||||
protected URLConnection openConnection(URL u) throws IOException { |
||||
return null; |
||||
} |
||||
}); |
||||
} catch (MalformedURLException ignored) {} |
||||
|
||||
if (url != null) { |
||||
host = url.getHost(); |
||||
path = url.getPath(); |
||||
|
||||
List<NameValuePair> pairs = URLUtils.parse(url.getQuery()); |
||||
for (NameValuePair pair: pairs) { |
||||
params.put(pair.getName(), pair.getValue()); |
||||
} |
||||
} |
||||
|
||||
FineLoggerFactory.getLogger().info("consume deep link: " + this.pendingURL); |
||||
performDeepLinks(this.pendingURL, host, path, params); |
||||
|
||||
markPendingURLConsumed(); |
||||
} |
||||
|
||||
private void performDeepLinks(String url, String host, String path, Map<String, Object> params) { |
||||
SwingUtilities.invokeLater(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
for (DeepLink deepLink: deepLinkList) { |
||||
if (deepLink.accept(url, host, path, params)) { |
||||
deepLink.run(url, host, path, params); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public String[] createNewArgs(String[] args) { |
||||
String filePath = this.pendingURL; |
||||
if (OperatingSystem.isMacos() && StringUtils.isNotEmpty(filePath) && new File(filePath).exists()) { |
||||
List<String> argList = new ArrayList<>(Arrays.asList(args)); |
||||
argList.add(filePath); |
||||
markPendingURLConsumed(); |
||||
return argList.toArray(new String[]{}); |
||||
} else { |
||||
return args; |
||||
} |
||||
} |
||||
public void start(String[] args) { |
||||
DeepLinkCore.getInstance().register(new FileOpen4MacDeepLink()); |
||||
DeepLinkCore.getInstance().register(new TemplateThemeInstallationDeepLink()); |
||||
|
||||
private void markPendingURLConsumed() { |
||||
this.pendingURL = null; |
||||
DeepLinkCore.getInstance().start(args); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue