Created
December 13, 2019 09:43
-
-
Save Zamay/9e93419a50bdd2251ff18c1052b4d647 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.appcompat.app.AppCompatActivity; | |
import android.annotation.TargetApi; | |
import android.app.ProgressDialog; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.webkit.URLUtil; | |
import android.webkit.WebResourceRequest; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.widget.Toast; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import com.facebook.FacebookSdk; | |
import com.facebook.applinks.AppLinkData; | |
import bolts.AppLinks; | |
import android.util.Log; | |
import java.util.Date; | |
import java.util.TimeZone; | |
public class MainActivity extends AppCompatActivity { | |
private WebView webView; | |
private SharedPreferences sharedPreferences; | |
private Handler statusUpdateHandler = new Handler(); | |
private Runnable statusUpdateRunnable; | |
private final Number LOADED_PAGE = 1; | |
private final String WHITE_PAGE_URL = "file:///android_asset/index.html"; | |
private final String URL_PREFIX = "https://app.neasna.online/api/entry"; | |
private boolean backPressedOnce = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
FacebookSdk.sdkInitialize(this); | |
OneSignal.startInit(this) | |
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification) | |
.unsubscribeWhenNotificationsAreDisabled(true) | |
.init(); | |
webView = findViewById(R.id.webView); | |
setWebView(); | |
sharedPreferences = getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE); | |
String loadedUrl = sharedPreferences.getString(LOADED_PAGE.toString(), null); | |
if (loadedUrl != null) { | |
Log.w("debug", "loadedUrl found in shared preferences"); | |
webView.loadUrl(loadedUrl); | |
} else { | |
obtainDeepLink(); | |
} | |
} | |
private void obtainDeepLink() | |
{ | |
Uri targetUrl = AppLinks.getTargetUrlFromInboundIntent(getApplicationContext(), getIntent()); | |
if (targetUrl != null) { | |
loadUrlWithDeepLink(targetUrl.toString()); | |
} else { | |
AppLinkData.fetchDeferredAppLinkData(this, | |
new AppLinkData.CompletionHandler() { | |
@Override | |
public void onDeferredAppLinkDataFetched(final AppLinkData appLinkData) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
String deepLink = null; | |
if (appLinkData != null && appLinkData.getTargetUri() != null) { | |
deepLink = appLinkData.getTargetUri().toString(); | |
} | |
loadUrlWithDeepLink(deepLink); | |
} | |
}); | |
} | |
} | |
); | |
} | |
} | |
private void loadUrlWithDeepLink(String deepLink) { | |
final ProgressDialog progressDialog = new ProgressDialog(this); | |
progressDialog.setMessage("Loading ..."); | |
progressDialog.setCancelable(false); | |
progressDialog.show(); | |
String url = Uri.parse(URL_PREFIX) | |
.buildUpon() | |
.appendQueryParameter("package", BuildConfig.APPLICATION_ID) | |
.appendQueryParameter("timezone", TimeZone.getDefault().getID()) | |
.appendQueryParameter("timezoneOffset", String.valueOf(TimeZone.getDefault().getOffset(new Date().getTime()) / 3600000)) | |
.appendQueryParameter("utm_content", (deepLink != null ? deepLink : "") ) | |
.build().toString(); | |
Log.w("debug", "Requiring... " + url); | |
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, | |
new Response.Listener<String>() { | |
@Override | |
public void onResponse(String url) { | |
progressDialog.dismiss(); | |
if (URLUtil.isValidUrl(url)) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.putString(LOADED_PAGE.toString(), url); | |
editor.apply(); | |
Log.w("debug", "Got url: " + url); | |
webView.loadUrl(url); | |
} else { | |
Log.w("debug", "Invalid url got: " + url); | |
webView.loadUrl(WHITE_PAGE_URL); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
progressDialog.dismiss(); | |
Log.w("debug", "Request is failed"); | |
webView.loadUrl(WHITE_PAGE_URL); | |
} | |
}); | |
RequestQueue queue = Volley.newRequestQueue(this); | |
queue.add(stringRequest); | |
} | |
private void setWebView() { | |
WebSettings settings = webView.getSettings(); | |
settings.setJavaScriptEnabled(true); | |
settings.setDomStorageEnabled(true); | |
settings.setAllowUniversalAccessFromFileURLs(true); | |
settings.setAllowFileAccessFromFileURLs(true); | |
settings.setAppCacheEnabled(true); | |
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); | |
webView.setWebViewClient(new WebViewClient() { | |
@TargetApi(Build.VERSION_CODES.N) | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { | |
view.loadUrl(request.getUrl().toString()); | |
return true; | |
} | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
view.loadUrl(url); | |
return true; | |
} | |
}); | |
} | |
public void onBackPressed() { | |
if (webView.canGoBack()) { | |
webView.goBack(); | |
} else { | |
if (backPressedOnce) { | |
super.onBackPressed(); | |
} | |
backPressedOnce = true; | |
final Toast toast = Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT); | |
toast.show(); | |
statusUpdateRunnable = new Runnable() { | |
@Override | |
public void run() { | |
backPressedOnce = false; | |
toast.cancel(); | |
} | |
}; | |
statusUpdateHandler.postDelayed(statusUpdateRunnable, 2000); | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (statusUpdateHandler != null) { | |
statusUpdateHandler.removeCallbacks(statusUpdateRunnable); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment