Android Open Source - notify Main Activity






From Project

Back to project page notify.

License

The source code is released under:

GNU General Public License

If you think the Android project notify listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.notify;
//  www.  jav  a 2s. com
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.Menu;

public class MainActivity extends Activity {
    public static mHandler mh;
    public static String status;
    public static Broadcast br = new Broadcast();
    public static MainActivity ma;
    public static DisplayFragments df;
    public static String savedIp;
    public static Boolean isOpen = false;
    public final Pattern p = Pattern.compile("^notify://\\d+\\.\\d+\\.\\d+\\.\\d+(:\\d+)?$");
    public BackgroundService ba = new BackgroundService();
    public FragmentManager fm;
    public Boolean accessibilityWaiting = false;
    public SharedPreferences pr;
    public SharedPreferences.Editor pre;
    public ArrayList<Fragment> displayList = new ArrayList<Fragment>();

    public MainActivity() {
        MainActivity.ma = this;
        if (MainActivity.mh == null) {
            MainActivity.mh = new mHandler();
        }
        BackgroundService.ma = this;
        AccessibilityListenerService.ba = this.ba;
        this.fm = getFragmentManager();
        MainActivity.df = new DisplayFragments();
    }

    protected void onResume() {
        super.onResume();
        this.isOpen = true;
        Integer accessibilityEnabled = null;
        try {
            accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        if (accessibilityEnabled == 0) {
            MainActivity.df.displayAccessibility();
        }
        ConnectivityManager cn = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo ni = cn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!ni.isConnected()) {
            MainActivity.df.displayWifiWarning();
        }

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

        if (!MainActivity.isOpen) {
            MainActivity.isOpen = true;
        }

        this.pr = getPreferences(Context.MODE_PRIVATE);
        this.pre = pr.edit();
        this.savedIp = pr.getString("ip", "");
        Intent intent = getIntent();
        if (this.handleIntent(intent.getDataString())) {
            return;
        }

        if (MainActivity.status == "connected") {
            MainActivity.df.displayConnected();
        } else {
            MainActivity.df.displayConnect();
        }
    }

    public Boolean handleIntent(String s) {
        if (s != null) {
            System.out.println(3);
            Matcher m = p.matcher(s);
            if (m.find()) {
                startConnection(Uri.parse(s));
                return true;
            } else {
                MainActivity.df.displayQrcodeWrong();
                return false;
            }
        }
        return false;
    }

    public void onBackPressed() {
        if (this.displayList.get(this.displayList.size() - 1).getClass().toString().equals(FragmentConnected.class.toString())) {
            AlertDialog.Builder ad = new AlertDialog.Builder(this);
            ad.setTitle("Disconnect?");
            ad.setMessage("Are you sure you want to disconnect?");
            ad.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    try {
                        BackgroundService.reading = false;
                        BackgroundService.dis.close();
                        MainActivity.df.displayConnect();
                    } catch (Exception e) {

                    }
                }
            });
            ad.setNegativeButton("no", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            ad.create().show();
            return;
        }
        if (this.displayList.size() == 1) {
            finish();
        } else {
            this.displayList.remove(this.displayList.size() - 1);
            MainActivity.df.setFragment(this.displayList.get(this.displayList.size() - 1), false);
        }
    }

    public void onActivityResult(int requestedCode, int resultCode, Intent intent) {
        if (requestedCode == 1) {
            System.out.println(1);
            if (resultCode == RESULT_OK) {
                System.out.println(2);
                this.handleIntent(intent.getStringExtra("SCAN_RESULT"));
            }
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void startConnection(Uri host) {
        Intent si = new Intent(this, BackgroundService.class);
        si.setData(host);
        startService(si);
        MainActivity.status = "connecting";

    }

    public void onDestroy() {
        super.onDestroy();
        if (MainActivity.status != "connected") {
            MainActivity.status = "";
        }
    }

    public void onStop() {
        super.onStop();
        this.isOpen = false;
    }

    class mHandler extends Handler {
        public void handleMessage(Message m) {
            try {
                Object[] s = (Object[])m.obj;
                switch((EnumMessage)s[0]) {
                    case connection_succeeded:
                        MainActivity.status = "connected";
                        MainActivity.df.displayConnected();
                        break;
                    case connection_failed:
                        MainActivity.status = "failed";
                        MainActivity.df.displayReconnect(false);
                        break;
                    case connection_died:
                        MainActivity.status = "died";
                        MainActivity.df.displayReconnect(true);
                        break;
                    case broadcast_response:
                        if (!BackgroundService.reading) {
                            BackgroundService.s = MainActivity.this.br.s;
                            Uri u = Uri.parse("notify://" + BackgroundService.s.getRemoteSocketAddress().toString().split(":")[0].replaceAll("^/", "") + ":18081");
                            MainActivity.this.startConnection(u);
                        }
                        break;
                    case broadcast_listen_failed:
                        MainActivity.status = "broadcast_failed";
                        MainActivity.df.displayBroadcastReconnect();
                        break;
                    default:
                        break;
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}




Java Source Code List

com.example.notify.AccessibilityListenerService.java
com.example.notify.BackgroundService.java
com.example.notify.Broadcast.java
com.example.notify.DisplayFragments.java
com.example.notify.EnumMessage.java
com.example.notify.FragmentAccessibility.java
com.example.notify.FragmentBroadcastReconnect.java
com.example.notify.FragmentConnect.java
com.example.notify.FragmentConnected.java
com.example.notify.FragmentConnecting.java
com.example.notify.FragmentManual.java
com.example.notify.FragmentNoQrcodeScanner.java
com.example.notify.FragmentQrcodeWrong.java
com.example.notify.FragmentReconnect.java
com.example.notify.FragmentWifiWarning.java
com.example.notify.IntentBroadcastReceiver.java
com.example.notify.MainActivity.java
com.example.notify.OurUtils.java