Skip to content

Instantly share code, notes, and snippets.

@jerefrer
Last active October 14, 2015 14:58
Show Gist options
  • Select an option

  • Save jerefrer/0098785849a6307e5f57 to your computer and use it in GitHub Desktop.

Select an option

Save jerefrer/0098785849a6307e5f57 to your computer and use it in GitHub Desktop.

Revisions

  1. jerefrer revised this gist Oct 14, 2015. No changes.
  2. jerefrer revised this gist Oct 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ParsePushCustomBroadcastReceiver.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    package com.sharenplay.sharenplay;
    package com.myapp.myapp;

    import android.content.Context;
    import android.content.Intent;
  3. jerefrer created this gist Oct 14, 2015.
    51 changes: 51 additions & 0 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.myapp" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:protectionLevel="signature" android:name="com.parse.starter.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.myapp.myapp.permission.C2D_MESSAGE" />

    <application
    android:name="com.myapp.myapp.Application"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <activity
    android:name=".MainActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    <service android:name="com.parse.PushService" />
    <receiver android:name=".ParsePushCustomBroadcastReceiver"
    android:exported="false">
    <intent-filter>
    <action android:name="com.parse.push.intent.RECEIVE" />
    <action android:name="com.parse.push.intent.DELETE" />
    <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
    </receiver>
    <receiver android:name="com.parse.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

    <category android:name="com.myapp.myapp" />
    </intent-filter>
    </receiver>
    </application>

    </manifest>
    13 changes: 13 additions & 0 deletions Application.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    package com.myapp.myapp;

    import com.parse.Parse;

    public class Application extends android.app.Application {

    @Override
    public void onCreate() {
    super.onCreate();
    Parse.initialize(this, "HIDDEN", "HIDDEN");
    }

    }
    20 changes: 20 additions & 0 deletions DetectConnection.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    package com.myapp.myapp;

    import android.content.Context;
    import android.net.ConnectivityManager;

    public class DetectConnection {
    public static boolean checkInternetConnection(Context context) {

    ConnectivityManager con_manager = (ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (con_manager.getActiveNetworkInfo() != null
    && con_manager.getActiveNetworkInfo().isAvailable()
    && con_manager.getActiveNetworkInfo().isConnected()) {
    return true;
    } else {
    return false;
    }
    }
    }
    148 changes: 148 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,148 @@
    package com.myapp.myapp;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.CookieManager;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Button;
    import android.widget.ProgressBar;

    import com.parse.ParseInstallation;

    public class MainActivity extends Activity {

    private Activity activity;
    private WebView webView;
    private View noInternetImage, splashImage;
    private ProgressBar progressBar;
    private Button retryButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    activity = this;
    splashImage = findViewById(R.id.splashImage);
    progressBar = (ProgressBar)findViewById(R.id.progressBar);
    webView = (WebView)findViewById(R.id.webview);
    noInternetImage = findViewById(R.id.noInternetImage);
    retryButton = (Button)findViewById(R.id.retryButton);

    webView.setBackgroundColor(Color.parseColor("#1e2b3e"));
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new MyWebViewClient());
    webView.setWebChromeClient(new WebChromeClient());

    if (!DetectConnection.checkInternetConnection(activity)) {
    showNoInternetScreen();
    } else {
    String url = getUrlFromParamsOrDefault();
    webView.loadUrl(url);
    }
    }

    private class MyWebViewClient extends WebViewClient
    {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (!DetectConnection.checkInternetConnection(activity)) {
    showNoInternetScreen();
    } else {
    view.loadUrl(url);
    }
    return true;
    }

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

    @Override
    public void onPageFinished(final WebView view, final String url) {
    setParseUser(url);
    showWebView();
    super.onPageFinished(view, url);
    }

    public void setParseUser(final String url) {
    String userId = getCookie(url, "snp_notification_id");
    if (userId != null) {
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("userId", userId);
    installation.saveInBackground();
    }
    }

    public String getCookie(String siteName, String cookieName){
    CookieManager cookieManager = CookieManager.getInstance();
    String cookiesString = CookieManager.getInstance().getCookie(siteName);
    String[] cookiesArray = cookiesString.split(";");
    for (String cookie : cookiesArray ){
    String[] keyValueArray = cookie.split("=");
    String key = keyValueArray[0];
    String value = keyValueArray[1];
    if (key.equals(cookieName)) return value;
    }
    return null;
    }
    }

    public void reloadWebView(View view) {
    if (!DetectConnection.checkInternetConnection(activity)) {
    showNoInternetScreen();
    } else {
    showLoadingScreen();
    String url = getUrlFromParamsOrDefault();
    webView.loadUrl(url);
    }
    }

    private void showWebView() {
    webView.setVisibility(View.VISIBLE);
    noInternetImage.setVisibility(View.GONE);
    retryButton.setVisibility(View.GONE);
    splashImage.setVisibility(View.GONE);
    progressBar.setVisibility(View.GONE);
    }

    private void showLoadingScreen() {
    webView.setVisibility(View.GONE);
    noInternetImage.setVisibility(View.GONE);
    retryButton.setVisibility(View.GONE);
    splashImage.setVisibility(View.VISIBLE);
    progressBar.setVisibility(View.VISIBLE);
    }

    private void showNoInternetScreen() {
    noInternetImage.setVisibility(View.VISIBLE);
    retryButton.setVisibility(View.VISIBLE);
    splashImage.setVisibility(View.GONE);
    progressBar.setVisibility(View.GONE);
    webView.setVisibility(View.GONE);
    }

    private String getUrlFromParamsOrDefault() {
    String url = "https://www.example.com";
    Bundle extras = getIntent().getExtras();
    if (extras != null) { url = extras.getString("url"); }
    return url;
    }

    @Override
    public void onBackPressed() {
    if (webView != null && webView.canGoBack()) {
    webView.goBack();
    } else {
    super.onBackPressed();
    }
    }

    }
    29 changes: 29 additions & 0 deletions ParsePushCustomBroadcastReceiver.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    package com.sharenplay.sharenplay;

    import android.content.Context;
    import android.content.Intent;

    import com.parse.ParsePushBroadcastReceiver;

    import org.json.JSONException;
    import org.json.JSONObject;

    public class ParsePushCustomBroadcastReceiver extends ParsePushBroadcastReceiver {

    @Override
    public void onPushOpen(Context context, Intent intent) {
    String url = "";
    String json = intent.getExtras().getString("com.parse.Data");
    try {
    JSONObject params = new JSONObject(json);
    url = params.getString("url");
    } catch (JSONException e) {
    e.printStackTrace();
    }
    Intent i = new Intent(context, MainActivity.class);
    i.putExtra("url", url);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
    }

    }
    48 changes: 48 additions & 0 deletions main_activity.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mains"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#1e2b3e">
    <ImageView
    android:id="@+id/splashImage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="visible"
    android:src="@drawable/splash" />
    <ProgressBar
    android:id="@+id/progressBar"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="201dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
    <ImageView
    android:id="@+id/noInternetImage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    android:src="@drawable/no_internet" />
    <WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Réessayer"
    android:id="@+id/retryButton"
    android:layout_marginBottom="70dp"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:textSize="20dp"
    android:visibility="gone"
    android:onClick="reloadWebView"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
    </RelativeLayout>