Android Open Source - garageoPIner-androidApp Main Fragment






From Project

Back to project page garageoPIner-androidApp.

License

The source code is released under:

Apache License

If you think the Android project garageoPIner-androidApp 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.wirthual.garageopiner.fragments;
//w  w w.  j  a v a 2  s.c  o  m
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.wirthual.garageopiner.R;
import com.wirthual.garageopiner.communication.CommunicationService;
import com.wirthual.garageopiner.utils.WifiChangedReceiver;

public class MainFragment extends Fragment implements
    android.view.View.OnClickListener {
  


  Button btntrigger;
  Button btntriggertime;
  
  NotificationCompat.Builder mBuilder;
  ConnectivityManager cm;

  SharedPreferences prefs;
  NetworkInfo mWifi;
  
  BroadcastReceiver wifiChanged;
  IntentFilter intentFilter;
  
  private Handler handler;
  private Runnable runnable;
  
  public MainFragment() {
  }


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    
    intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
 
    wifiChanged = new BroadcastReceiver() {
 
      @Override
      public void onReceive(Context context, Intent intent) {
         final String action = intent.getAction();
            if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
                  //Wait until wifi is really connected not just turned on
                  handler.postDelayed(runnable, 1000);
                } else {
                  //Wifi turned off, disable buttons imediately
                  HandleWifiState(false);
                }
          }
      }
    };
    
     runnable = new Runnable() {
         @Override
         public void run() {
          if(!isConnectedViaWifi()) {
              //if not connected, calls itself again
                handler.postDelayed(this, 100);
          }else{
            //If connected, enable buttons
            HandleWifiState(true);   
          }            
         }
      };
    
     handler = new Handler();
    
    View rootView = inflater.inflate(R.layout.fragment_main, container,
        false);

    prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

    btntrigger = (Button) rootView.findViewById(R.id.btn_trigger);
    btntrigger.setOnClickListener(this);
    btntrigger.setText(R.string.btntxt_trigger);
    btntriggertime = (Button) rootView.findViewById(R.id.btn_triggertime);
    btntriggertime.setOnClickListener(this);

    return rootView;
  }

  @Override
  public void onResume() {
    super.onResume();
    
    mWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        
    boolean timeControl = prefs.getBoolean("timecontrol", false);
    if (!timeControl) {
      btntriggertime.setVisibility(View.INVISIBLE);
    } else {
      btntriggertime.setVisibility(View.VISIBLE);
      String time = prefs.getString("time", "2 Minuten");
      String triggertime = this.getString(R.string.btntxt_triggertime)+ " " + time;
      
      btntriggertime.setText(triggertime);
    }
    
    HandleWifiState(mWifi.isConnected());
    
    //registering our receiver
    this.getActivity().registerReceiver(wifiChanged, intentFilter);

      }

  @Override
  public void onClick(View v) {
    Intent i = null;
    switch (v.getId()) {
    case R.id.btn_trigger:
      i = new Intent(getActivity(), CommunicationService.class);
      i.setAction(CommunicationService.ACTION_TOGGLE);
      getActivity().getApplicationContext().startService(i);
      break;
    case R.id.btn_triggertime:
      i = new Intent(getActivity(), CommunicationService.class);
      i.setAction(CommunicationService.ACTION_TIMERSTART);
      getActivity().getApplicationContext().startService(i);
      break;
    default:
      break;
    }
  }
  
  @Override
  public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    //unregister our receiver
    this.getActivity().unregisterReceiver(this.wifiChanged);
  }
  
  void HandleWifiState(boolean connected){
    
        if (connected) {
          btntrigger.setEnabled(true);
          btntriggertime.setEnabled(true);
        }else{
          btntrigger.setEnabled(false);
          btntriggertime.setEnabled(false);
        }
  }
  
    private boolean isConnectedViaWifi() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);     
        return mWifi.isConnected();
   }


}




Java Source Code List

com.wirthual.garageopiner.activities.MainActivity.java
com.wirthual.garageopiner.activities.SettingsActivity.java
com.wirthual.garageopiner.activities.TimeTableActivity.java
com.wirthual.garageopiner.communication.CommunicationService.java
com.wirthual.garageopiner.communication.DisplayToast.java
com.wirthual.garageopiner.communication.GarageoPInerHTTPClient.java
com.wirthual.garageopiner.fragments.MainFragment.java
com.wirthual.garageopiner.fragments.TimeTableFragment.java
com.wirthual.garageopiner.utils.ExpandableListAdapter.java
com.wirthual.garageopiner.utils.MyTimeTriggerWidgetProvider.java
com.wirthual.garageopiner.utils.MyTriggerWidgetProvider.java
com.wirthual.garageopiner.utils.NotificationUpdateRunnable.java
com.wirthual.garageopiner.utils.WifiChangedReceiver.java