Skip to content

Commit

Permalink
Fix App Not Loading (amahi#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-jn committed Jul 13, 2020
1 parent adad44d commit 303f0df
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions src/main/java/org/amahi/anywhere/activity/ServerAppActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -31,6 +30,13 @@
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import org.amahi.anywhere.AmahiApplication;
import org.amahi.anywhere.R;
import org.amahi.anywhere.server.client.ServerClient;
Expand All @@ -54,6 +60,8 @@ public class ServerAppActivity extends AppCompatActivity {
@Inject
ServerClient serverClient;

Context ctx;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -67,6 +75,7 @@ protected void onCreate(Bundle savedInstanceState) {

private void setUpInjections() {
AmahiApplication.from(this).inject(this);
ctx = this;
}

private void setUpApp(Bundle state) {
Expand All @@ -83,18 +92,27 @@ private void setUpAppWebCookie() {
String appCookies = Preferences.ofCookie(this).getAppCookies(appHost);

for (String appCookie : TextUtils.split(appCookies, ";")) {
CookieManager.getInstance().setCookie(getAppUrl(), appCookie);
CookieManager.getInstance().setCookie(getServerUrl(), appCookie);
}
}

private ServerApp getApp() {
return getIntent().getParcelableExtra(Intents.Extras.SERVER_APP);
}

private String getAppUrl() {
private String getServerUrl() {
return serverClient.getServerAddress();
}

private String getAppUrl() {
String host = getApp().getHost();
if (host.matches("^(http|https)://")) {
return host;
} else {
return "http://" + host;
}
}

private void setUpAppWebAgent() {
getWebView().getSettings().setUserAgentString(getAppWebAgent());
}
Expand Down Expand Up @@ -130,6 +148,18 @@ private void setUpAppWebTitle() {
getSupportActionBar().setTitle(getApp().getName());
}

private void setUpAppWebIcon() {
getSupportActionBar().setIcon(R.drawable.ic_launcher);
if (!TextUtils.isEmpty(getApp().getLogoUrl())) {
Glide.with(ctx).load(getApp().getLogoUrl()).into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
getSupportActionBar().setIcon(resource);
}
});
}
}

private void setUpAppWebContent(Bundle state) {
if (state == null) {
getWebView().loadUrl(getAppUrl());
Expand Down Expand Up @@ -192,9 +222,8 @@ protected void onPause() {

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

getWebView().saveState(outState);
super.onSaveInstanceState(outState);
}

@Override
Expand All @@ -210,7 +239,7 @@ protected void onDestroy() {

private void tearDownAppWebCookie() {
String appHost = getApp().getHost();
String appCookies = CookieManager.getInstance().getCookie(getAppUrl());
String appCookies = CookieManager.getInstance().getCookie(getServerUrl());

Preferences.ofCookie(this).setAppCookies(appHost, appCookies);

Expand All @@ -227,6 +256,11 @@ private void showApp() {
ViewDirector.of(this, R.id.animator).show(R.id.web_content);
}

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LocaleHelper.onAttach(newBase));
}

private static final class AppWebAgentField {
public static final String HOST = "Vhost";

Expand Down Expand Up @@ -255,9 +289,4 @@ public void onPageFinished(WebView appWebView, String appUrl) {
activity.showApp();
}
}

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LocaleHelper.onAttach(newBase));
}
}

0 comments on commit 303f0df

Please sign in to comment.