Created
December 16, 2019 16:09
-
-
Save Zamay/29d913c79f011cf3662dfd333dd5d48f 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
package com.musocal.testfblink; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.annotation.SuppressLint; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.os.Handler; | |
import android.provider.MediaStore; | |
import android.view.KeyEvent; | |
import android.webkit.URLUtil; | |
import android.webkit.ValueCallback; | |
import android.webkit.WebChromeClient; | |
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.io.File; | |
import java.io.IOException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Locale; | |
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.sgenac.club/api/entry"; | |
private boolean backPressedOnce = false; | |
private String mCM; | |
private ValueCallback<Uri> mUM; | |
private ValueCallback<Uri[]> mUMA; | |
private final static int FCR = 1; | |
@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("language", Locale.getDefault().getLanguage() ) | |
.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)) { | |
if (url == WHITE_PAGE_URL){ | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.putString(LOADED_PAGE.toString(), url); | |
editor.apply(); | |
Log.w("debug", "Saved url: " + url); | |
} | |
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.setAllowFileAccess(true); | |
settings.setAllowContentAccess(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; | |
} | |
}); | |
webView.setWebChromeClient(new WebChromeClient() { | |
public boolean onShowFileChooser(WebView webView, | |
ValueCallback<Uri[]> filePathCallback, | |
WebChromeClient.FileChooserParams fileChooserParams) { | |
if (mUMA != null) { | |
mUMA.onReceiveValue(null); | |
} | |
mUMA = filePathCallback; | |
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) { | |
File photoFile = null; | |
try { | |
photoFile = createImageFile(); | |
takePictureIntent.putExtra("PhotoPath", mCM); | |
} catch (IOException ex) { | |
Log.e("Webview", "Image file creation failed", ex); | |
} | |
if (photoFile != null) { | |
mCM = "file:" + photoFile.getAbsolutePath(); | |
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); | |
} else { | |
takePictureIntent = null; | |
} | |
} | |
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); | |
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); | |
contentSelectionIntent.setType("*/*"); | |
Intent[] intentArray; | |
if (takePictureIntent != null) { | |
intentArray = new Intent[]{takePictureIntent}; | |
} else { | |
intentArray = new Intent[0]; | |
} | |
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); | |
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); | |
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); | |
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); | |
startActivityForResult(chooserIntent, FCR); | |
return true; | |
} | |
}); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { | |
super.onActivityResult(requestCode, resultCode, intent); | |
if (Build.VERSION.SDK_INT >= 21) { | |
Uri[] results = null; | |
//Check if response is positive | |
if (resultCode == Activity.RESULT_OK) { | |
if (requestCode == FCR) { | |
if (null == mUMA) { | |
return; | |
} | |
if (intent == null) { | |
//Capture Photo if no image available | |
if (mCM != null) { | |
results = new Uri[]{Uri.parse(mCM)}; | |
} | |
} else { | |
String dataString = intent.getDataString(); | |
if (dataString != null) { | |
results = new Uri[]{Uri.parse(dataString)}; | |
} | |
} | |
} | |
} | |
mUMA.onReceiveValue(results); | |
mUMA = null; | |
} else { | |
if (requestCode == FCR) { | |
if (null == mUM) return; | |
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); | |
mUM.onReceiveValue(result); | |
mUM = null; | |
} | |
} | |
} | |
private File createImageFile() throws IOException { | |
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); | |
String imageFileName = "img_" + timeStamp + "_"; | |
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); | |
return File.createTempFile(imageFileName, ".jpg", storageDir); | |
} | |
@Override | |
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) { | |
if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
switch (keyCode) { | |
case KeyEvent.KEYCODE_BACK: | |
if (webView.canGoBack()) { | |
webView.goBack(); | |
} else { | |
finish(); | |
} | |
return true; | |
} | |
} | |
return super.onKeyDown(keyCode, event); | |
} | |
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