Skip to content

Commit

Permalink
Fixed logic for custom URLs, added missing support for evo: and evos:
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikael Kindborg committed Feb 11, 2016
1 parent 39c46b2 commit 174c423
Showing 1 changed file with 92 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,24 @@ public EvothingsWebViewClient(
mActivity = activity;
}

/**
* Called when fetching resources, return custom data or null for default
* handling.
*/
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
{
//LOG.i("EvothingsWebViewClient", "shouldInterceptRequest "+url);

// Check if this is a Cordova file being leader and handle accordingly.
String localURL = getCordovaLocalFileURL(url);
if (null != localURL)
{
return handleCordovaURL(view, Uri.parse(localURL), url);
}
else if (url.startsWith("evothings:"))
{
// Replace the 'evothings' protocol with 'http'.
url = "http" + url.substring(9);
}

// If we're running a cached app, limit access to the rest of the file system.
else if (mCachedApp != null && url.startsWith("file:"))
if (mCachedApp != null && url.startsWith("file:"))
{
if (!url.startsWith(mCachedApp))
{
Expand All @@ -232,9 +234,10 @@ else if (mCachedApp != null && url.startsWith("file:"))
return new WebResourceResponse("text/plain", "UTF-8", null);
}
}

// Prevent origins that aren't our own start page from
// accessing these resources.
else if (url.startsWith("evocachemeta:"))
if (url.startsWith("evocachemeta:"))
{
if (!ensureStartPage(url))
{
Expand All @@ -251,7 +254,7 @@ else if (url.startsWith("evocachemeta:"))
"UTF-8",
new ByteArrayInputStream(generateClientsAppListJson()));
}
catch(Exception e)
catch (Exception e)
{
e.printStackTrace();
// no need to do anything else here.
Expand All @@ -261,70 +264,59 @@ else if (url.startsWith("evocachemeta:"))
{
LOG.e("EvothingsWebViewClient", "evocachemeta unhandled: "+url);
}

// TODO: add a command for removing apps.
}

return null;
}

private void createAppListJson(File file)
{
try
{
FileWriter w = new FileWriter(file);
w.write("{}");
w.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
mLoadedPage = url;
super.onPageStarted(view, url, favicon);
}

// Returns true if the loaded page is the start page, false otherwise.
private boolean ensureStartPage(String url)
{
if (mLoadedPage == null)
{
LOG.e("EvothingsWebViewClient", "mLoadedPage null, "+url);
}
if (mLoadedPage.equals(Config.getStartUrl()))
{
return true;
}
else
{
LOG.e("EvothingsWebViewClient", "mLoadedPage "+mLoadedPage+", "+url);
}
return false;
}

/**
* Called when loading a page, return true to load page ourselves
* or false for default loading.
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
LOG.i("EvothingsWebViewClient", "shouldOverrideUrlLoading "+url);

String cacheRoot = mActivity.getDir("evocache",
MODE_PRIVATE).toURI().toString();
String cacheRoot =
mActivity
.getDir("evocache", MODE_PRIVATE)
.toURI()
.toString();

mCachedApp = null;

// Used by external apps to load things into Evothings Client.
// Used by external apps to load things into Evothings Viewer.
if (url.startsWith("evothings:"))
{
// Replace the 'evothings' protocol with 'http'.
url = "http" + url.substring(9);
appView.loadUrlIntoView(url, true);
return true; // we handled it.
}

// Used by external apps to load things into Evothings Viewer.
if (url.startsWith("evo:"))
{
// Replace the 'evothings' protocol with 'http'.
url = "http" + url.substring(3);
appView.loadUrlIntoView(url, true);
return true; // we handled it.
}

// Used by external apps to load things into Evothings Viewer.
if (url.startsWith("evos:"))
{
// Replace the 'evothings' protocol with 'http'.
url = "https" + url.substring(4);
appView.loadUrlIntoView(url, true);
return true; // we handled it.
}

// Load a cached app.
else if (url.startsWith(cacheRoot))
if (url.startsWith(cacheRoot))
{
Uri uri = Uri.parse(url);
if (!uri.getHost().equals(uri.getAuthority()))
Expand All @@ -337,33 +329,73 @@ else if (url.startsWith(cacheRoot))
LOG.e("EvothingsWebViewClient", "mCachedApp: "+mCachedApp);
return false; // shouldInterceptRequest will handle it.
}

// Cache a new app or update a cached app.
else if (url.startsWith("evocacheadd:"))
if (url.startsWith("evocacheadd:"))
{
new EvoCacheAddThread(url).start();
return true; // we'll handle it.
}

// Prevent origins that aren't our own start page from accessing
// these resources.
else if (url.startsWith("evocachemeta:"))
if (url.startsWith("evocachemeta:"))
{
if (!ensureStartPage(url))
{
return false;
}

String metaUrl = url.substring("evocachemeta:".length());
if (metaUrl.startsWith("delete/"))
{
String deleteRequestAppIndex = metaUrl.substring("delete/".length());
new EvoCacheDeleteThread(deleteRequestAppIndex).start();
return true;
}
else
{
return false;
}
}

return false; // system handles it.
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
mLoadedPage = url;
super.onPageStarted(view, url, favicon);
}

private void createAppListJson(File file)
{
try
{
FileWriter w = new FileWriter(file);
w.write("{}");
w.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

// Returns true if the loaded page is the start page, false otherwise.
private boolean ensureStartPage(String url)
{
if (mLoadedPage == null)
{
LOG.e("EvothingsWebViewClient", "mLoadedPage null, "+url);
return false;
}

if (mLoadedPage.equals(Config.getStartUrl()))
{
return true;
}
else
{
return false; // system handles it.
LOG.e("EvothingsWebViewClient", "mLoadedPage "+mLoadedPage+", "+url);
return false;
}
}

Expand All @@ -382,7 +414,7 @@ public void run()
{
evoCacheDelete(mIndex);
}
catch(Exception e)
catch (Exception e)
{
e.printStackTrace();
// Fatal error, let's kill the app.
Expand All @@ -406,7 +438,7 @@ public void run()
{
evoCacheAdd(mUrl);
}
catch(Exception e)
catch (Exception e)
{
e.printStackTrace();
// Fatal error, let's kill the app.
Expand Down

0 comments on commit 174c423

Please sign in to comment.