Android Open Source - android-barometer Fragment Barometer






From Project

Back to project page android-barometer.

License

The source code is released under:

GNU General Public License

If you think the Android project android-barometer 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 de.uvwxy.barometer;
// w  ww .j a v a 2  s .c o m
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import de.uvwxy.helper.BitmapTools;
import de.uvwxy.helper.IntentTools;
import de.uvwxy.sensors.BarometerReader;
import de.uvwxy.sensors.SensorReader.SensorResultCallback;

public class FragmentBarometer extends Fragment {
  protected final static String PREF_ID = "BARO_SETTINGS";
  private final static String PREF_BARO_OLD = "BARO_SETTINGS";

  private Context ctx = null;
  private ImageView ivBaro = null;
  private TextView tvInfo = null;

  private long lastTap = System.currentTimeMillis();
  private long tapSpeed = 500;

  private BarometerReader baroReader = null;
  private Barometer baro = null;
  private float oldValue;
  private float origValue;
  private float currentValue;

  private boolean save = false;

  int x = 0;
  private SensorResultCallback cb = new SensorResultCallback() {

    @Override
    public void result(float[] f) {
      if (f != null && f.length >= 1) {
        currentValue = f[0];
        origValue = currentValue;
        currentValue = FragmentSettings.getDiffedBaro(ctx, currentValue);

        // split in if else to avoid values changing after reading
        if (save) {
          oldValue = currentValue;
          writeValues();
          save = false;
          Toast.makeText(ctx, "Saved", Toast.LENGTH_SHORT).show();
        }

        Bitmap temp = getUnitsBitmap();

        int w = ivBaro.getWidth();
        int h = ivBaro.getHeight();
        int l = w <= h ? w : h;

        if (l != 0) {
          ivBaro.setImageBitmap(BitmapTools.scaleBitmap(temp, l, l, true));
        }
      }
    }
  };

  private void initGUI(View rootView) {
    ivBaro = (ImageView) rootView.findViewById(R.id.ivBaro);
    tvInfo = (TextView) rootView.findViewById(R.id.tvInfo);

    ivBaro.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (System.currentTimeMillis() - lastTap > 1250) {

          updateBaro();
          lastTap = System.currentTimeMillis();
        }
      }
    });

    ivBaro.setOnLongClickListener(new OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
        save = true;
        updateBaro();
        return true;
      }
    });
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_main, container, false);

    ctx = getActivity().getApplicationContext();
    super.onCreate(savedInstanceState);
    initGUI(rootView);
    baro = new Barometer();
    baro.loadBitmaps(ctx);
    baroReader = new BarometerReader(ctx, 50, cb);
    readValues();

    if (IntentTools.isFirstLaunch(ctx, PREF_ID)) {
      save = true;
    }

    return rootView;
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  }

  @Override
  public void onResume() {
    int w = ivBaro.getWidth();
    int h = ivBaro.getHeight();
    int l = w <= h ? w : h;
    if (l != 0) {
      ivBaro.setImageBitmap(BitmapTools.scaleBitmap(getUnitsBitmap(), l, l, true));
    }

    updateBaro();
    super.onResume();
  }

  private void updateBaro() {
    baroReader.startReading();
  }

  private void readValues() {
    SharedPreferences settings = ctx.getSharedPreferences(PREF_ID, 0);
    oldValue = settings.getFloat(PREF_BARO_OLD, -1);
  }

  private void writeValues() {
    SharedPreferences settings = ctx.getSharedPreferences(PREF_ID, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putFloat(PREF_BARO_OLD, currentValue);
    editor.commit();
  }

  private Bitmap getUnitsBitmap(){
    return baro.drawUnitsWithMemory(currentValue, oldValue, FragmentSettings.loadPressureUnit(getActivity()), FragmentSettings.loadLengthUnit(getActivity()));

  }

}




Java Source Code List

de.uvwxy.barometer.Barometer.java
de.uvwxy.barometer.FragmentBarometer.java
de.uvwxy.barometer.FragmentSettings.java
de.uvwxy.barometer.MainActivity.java
de.uvwxy.barometer.WidgetBarometer.java