Android Open Source - MobHere Mob Here Main Activity






From Project

Back to project page MobHere.

License

The source code is released under:

GNU General Public License

If you think the Android project MobHere 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 cl.mobiquos.mobhere;
/*from  w w w  .java  2 s .c  o m*/
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;



public class MobHereMainActivity extends ActionBarActivity implements LocationListener,SensorEventListener  {
  
  
  private ShareActionProvider mShareActionProvider;
  private Intent sharingIntent;

  //MANAGER
  public LocationManager locationManager;
  private SensorManager mSensorManager;

  //SENSORs
  public Location dataLocation;
  private Sensor mTemperature;
  private Sensor mHumidity;
  
  private final static String TAGNAME = "MainActivityHereIam";
  
  private TextView latitude;
  private TextView longitude;
  private TextView altitude;
  private TextView speed;
  private TextView provider;
  private TextView gps;
  private TextView accuracity;
  
  private TextView temp;
  private TextView humidity;
  
  private ToggleButton toggle;
  
  private LinearLayout layout;
  //private List<HerePoint> values;  
  
  
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        
        setContentView(R.layout.activity_main);
        //ActionBar action_bar = getActionBar();
        
       // values = new ArrayList<HerePoint>();
        
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        
        actionBar.show();
        //actionBar.setDisplayHomeAsUpEnabled(true); 
        
        
        /*Widgets*/
        latitude = (TextView)findViewById(R.id.latitude);
      longitude = (TextView)findViewById(R.id.longitude);
      altitude = (TextView)findViewById(R.id.altitude);
      speed = (TextView)findViewById(R.id.speed);
      provider = (TextView)findViewById(R.id.provider);
      gps = (TextView)findViewById(R.id.gps);
      accuracity = (TextView)findViewById(R.id.accuracity);
      temp = (TextView)findViewById(R.id.temp);
      humidity = (TextView)findViewById(R.id.hum);
      
      toggle = (ToggleButton)findViewById(R.id.toggleButtonOptions);
      layout = (LinearLayout)findViewById(R.id.additionalLayout);

      
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0,
                this);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Toast.makeText(this,getResources().getString(R.string.gpsok)
                ,Toast.LENGTH_SHORT).show();
            gettingGPSLocation();
            
        }else{
            showGPSDisabled();
        }
        
        if (toggle.isChecked()) {
        this.layout.setVisibility(LinearLayout.VISIBLE);
      }
      else {
        this.layout.setVisibility(LinearLayout.GONE);
      }
        
        //SENSORs
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        
        this.mTemperature = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
        this.mHumidity = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
        
    }
   
    public void gettingGPSLocation(){
      final ProgressDialog ringProgressDialog = ProgressDialog.show(MobHereMainActivity.this,
          getResources().getString(R.string.loading),
          getResources().getString(R.string.loading_gps), true);
      
              ringProgressDialog.setCancelable(true);
              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          // Here you should write your time consuming task...
                          // Let the progress ring for 10 seconds...
                          while (latitude.getText().toString().equals(getResources().getString(R.string.app_name))){
                            
                          }  
                          
                      } catch (Exception e) {
                      }
                      ringProgressDialog.dismiss();
                  }
              }).start();

    }
    
    public void showGPSDisabled(){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage(R.string.error_gps_disabled)
           .setTitle(R.string.title_alertdialog)
           .setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
               Intent GPSSett = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
               startActivity(GPSSett);
             }
           })
           .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
               Toast.makeText(MobHereMainActivity.this,getResources().getString(R.string.gpsoff)
                       ,Toast.LENGTH_SHORT).show();
             }
           });
    AlertDialog dialog = builder.create();
    dialog.show();

    }
    
    
    private void updateExtraOfShareIntent(){
      sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "App: Aqui estoy");
        sharingIntent.putExtra(Intent.EXTRA_TEXT,this.createMessageForShare());
    }
    
    private Intent getDefaultIntent() {
        this.sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        //intent.setPackage("com.whatsapp");
        sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        //sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "App: Aqui estoy");
        //sharingIntent.putExtra(Intent.EXTRA_TEXT,this.createMessageForShare());

        return this.sharingIntent;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_action_bar, menu);
      
      
   // Set up ShareActionProvider's default share intent
      MenuItem shareItem = menu.findItem(R.id.action_share);
      mShareActionProvider = (ShareActionProvider)
              MenuItemCompat.getActionProvider(shareItem);
      mShareActionProvider.setShareIntent(getDefaultIntent());
      
      
      return true;
    }   
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      // action with ID action_refresh was selected
      case R.id.action_email:
        sendMail();
        break;
      // action with ID action_settings was selected
      case R.id.action_map:
        openUrlMap();
        break;
      case R.id.action_mobiquos:
        dialogMobiquos();
        break;
      case R.id.action_contact:
        contactMobiquos();
        break;
      default:
        break;
      }

      return true;
    } 
    public void shareContent(View view){
      Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
      sharingIntent.setType("text/plain");
      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
      sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "GOLA");
      startActivity(sharingIntent);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      // Save UI state changes to the savedInstanceState.
      // This bundle will be passed to onCreate if the process is
      // killed and restarted.
      Log.i(TAGNAME, "In the onSaveInstanceState method");
      //savedInstanceState.putBoolean("togglebutton",this.toggle.isChecked());
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      // Restore UI state from the savedInstanceState.
      // This bundle has also been passed to onCreate.
      
      Log.i(TAGNAME, "In the onRestoreInstanceState method");
      if ( savedInstanceState.getBoolean("togglebutton") == true ){
        toggle.setChecked(true);
      }
      else {
        toggle.setChecked(false);
      }
      
    }   
    
    /**
     * 
     * @param view
     */
    public void viewDetail(View view){
      if (toggle.isChecked()) {
        this.layout.setVisibility(LinearLayout.VISIBLE);
      }
      else {
        this.layout.setVisibility(LinearLayout.GONE);
      }
    }
    
    
    public void OpenWeb(View view){
      final Intent intent = new Intent(Intent.ACTION_VIEW)
        .setData(Uri.parse(getResources().getString(R.string.company_url)));
        startActivity(intent);
    }
    
    public void contactMobiquos(){

      
      Intent email = new Intent(Intent.ACTION_SEND);
      email.putExtra(Intent.EXTRA_EMAIL, new String[]{"info@mobiquos.cl"});      
      email.putExtra(Intent.EXTRA_SUBJECT, "Contacto desde - App");
      //-message/rfc822
      email.setType("plain/text");
      startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }
    
    /**
     * 
     * @return String
     */
    private String createUrl(){
      String onlylat,onlylon;
      onlylat = latitude.getText().toString().replace(" ","");
      onlylon = longitude.getText().toString().replace(" ","");
      return "http://maps.google.com/maps?q=loc:"+ onlylat +"," + onlylon;
    }
    
    /**
     * 
     * @param view
     */
    public void openUrlMap(){
      
      String onlylat,onlylon;
      onlylat = latitude.getText().toString().replace(" ","");
      onlylon = longitude.getText().toString().replace(" ","");
      Intent navigation = new Intent(Intent.ACTION_VIEW, Uri
              .parse("http://maps.google.com/maps?q=loc:"
                  + onlylat +"," + onlylon));
      startActivity(navigation);
      
    }

    
    private String createMessageForShare(){
      String message;
      message = "Localizacion:";
      message = message + "Latitud:" + latitude.getText().toString() + "\n";
      message = message + "Longitud:" + longitude.getText().toString() + "\n";
      message = message + "Precision:" + accuracity.getText().toString() + "\n";
      message = message + "URL:"+createUrl();
      message = message + "\n--------------------------------------\n";
      message = message + "APP: <a>https://play.google.com/store/apps/details?id=cl.mobiquos.mobhere</a>";
      return message;
    }
    /**
     * 
     * @param view
     */
    public void sendMail(){
      String message;
      
      message = "Localizacion:";
      message = message + "Latitud:" + latitude.getText().toString() + "\n";
      message = message + "Longitud:" + longitude.getText().toString() + "\n";
      message = message + "Precision:" + accuracity.getText().toString() + "\n";
      message = message + "URL:"+createUrl();
      message = message + "\n--------------------------------------\n";
      
      Intent email = new Intent(Intent.ACTION_SEND);
      
      email.putExtra(Intent.EXTRA_SUBJECT, "App: Aqui estoy");
      email.putExtra(Intent.EXTRA_TEXT,message);
      email.setType("message/rfc822");
      startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }
   
    public void dialogMobiquos(){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);

      LayoutInflater inflater = this.getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.mobiquos_layout, null));
    builder.setTitle(R.string.company_name)
           .setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
             }
           });
    AlertDialog dialog = builder.create();
    dialog.show();
    }
    
    
  @Override
  public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    
    
    latitude.setText(String.format("%9.6f",location.getLatitude()));
      longitude.setText(String.format("%9.6f",location.getLongitude()));
      altitude.setText(String.format("%9.6f",location.getAltitude()));
      speed.setText(String.format("%9.6f",location.getSpeed()));
      provider.setText(location.getProvider());
      accuracity.setText(String.format("%9.6f",location.getAccuracy()));
      gps.setText(Integer.toString(location.getExtras().getInt("satellites")));
      
      updateExtraOfShareIntent();
      
  }


  @Override
  public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    
  }


  @Override
  public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
    
  }


  @Override
  public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub
    Log.i(TAGNAME, "On Status Changed method");
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
              3000, 0, this);
      
      mSensorManager.registerListener(this, mTemperature, SensorManager.SENSOR_DELAY_NORMAL);
      mSensorManager.registerListener(this, mHumidity, SensorManager.SENSOR_DELAY_NORMAL);
      Log.i(TAGNAME, "On Resumen method");
 
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(this);
      mSensorManager.unregisterListener(this);
      Log.i(TAGNAME, "On Pause method");
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      locationManager.removeUpdates(this);
      Log.i(TAGNAME, "On Destroy method");
  }

  @Override
  public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    this.temp.setText(String.valueOf(event.values[0]));
    this.temp.setText(String.valueOf(event.values[0]));
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
    
  }    
}




Java Source Code List

cl.mobiquos.mobhere.MainActivity.java
cl.mobiquos.mobhere.MobHereMainActivity.java