|
| 1 | +package org.fossasia.pslab.activity; |
| 2 | + |
| 3 | +import android.content.SharedPreferences; |
| 4 | +import android.hardware.Sensor; |
| 5 | +import android.hardware.SensorEvent; |
| 6 | +import android.hardware.SensorEventListener; |
| 7 | +import android.hardware.SensorManager; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.os.Handler; |
| 10 | +import android.support.annotation.NonNull; |
| 11 | +import android.support.design.widget.BottomSheetBehavior; |
| 12 | +import android.support.v7.app.AppCompatActivity; |
| 13 | +import android.support.v7.widget.Toolbar; |
| 14 | +import android.view.GestureDetector; |
| 15 | +import android.view.Menu; |
| 16 | +import android.view.MenuInflater; |
| 17 | +import android.view.MenuItem; |
| 18 | +import android.view.MotionEvent; |
| 19 | +import android.view.View; |
| 20 | +import android.view.animation.Animation; |
| 21 | +import android.view.animation.RotateAnimation; |
| 22 | +import android.widget.ImageView; |
| 23 | +import android.widget.LinearLayout; |
| 24 | +import android.widget.RadioButton; |
| 25 | +import android.widget.TextView; |
| 26 | + |
| 27 | +import org.fossasia.pslab.R; |
| 28 | +import org.fossasia.pslab.others.MathUtils; |
| 29 | +import org.fossasia.pslab.others.SwipeGestureDetector; |
| 30 | + |
| 31 | +import butterknife.BindView; |
| 32 | +import butterknife.ButterKnife; |
| 33 | + |
| 34 | +/** |
| 35 | + * Created by Harsh on 7/17/18. |
| 36 | + */ |
| 37 | + |
| 38 | +public class CompassActivity extends AppCompatActivity implements SensorEventListener { |
| 39 | + |
| 40 | + private static final String PREFS_NAME = "CompassPreference"; |
| 41 | + |
| 42 | + @BindView(R.id.compass) |
| 43 | + ImageView compass; |
| 44 | + @BindView(R.id.degree_indicator) |
| 45 | + TextView degreeIndicator; |
| 46 | + |
| 47 | + @BindView(R.id.compass_radio_button_x_axis) |
| 48 | + RadioButton xAxisRadioButton; |
| 49 | + @BindView(R.id.compass_radio_button_y_axis) |
| 50 | + RadioButton yAxisRadioButton; |
| 51 | + @BindView(R.id.compass_radio_button_z_axis) |
| 52 | + RadioButton zAxisRadioButton; |
| 53 | + |
| 54 | + @BindView(R.id.tv_sensor_hmc5883l_bx) |
| 55 | + TextView xAxisMagneticField; |
| 56 | + @BindView(R.id.tv_sensor_hmc5883l_by) |
| 57 | + TextView yAxisMagneticField; |
| 58 | + @BindView(R.id.tv_sensor_hmc5883l_bz) |
| 59 | + TextView zAxismagneticField; |
| 60 | + |
| 61 | + @BindView(R.id.compass_toolbar) |
| 62 | + Toolbar mToolbar; |
| 63 | + |
| 64 | + @BindView(R.id.bottom_sheet) |
| 65 | + LinearLayout bottomSheet; |
| 66 | + @BindView(R.id.shadow) |
| 67 | + View tvShadow; |
| 68 | + @BindView(R.id.img_arrow) |
| 69 | + ImageView arrowUpDown; |
| 70 | + @BindView(R.id.sheet_slide_text) |
| 71 | + TextView bottomSheetSlideText; |
| 72 | + @BindView(R.id.guide_title) |
| 73 | + TextView bottomSheetGuideTitle; |
| 74 | + @BindView(R.id.custom_dialog_text) |
| 75 | + TextView bottomSheetText; |
| 76 | + @BindView(R.id.custom_dialog_schematic) |
| 77 | + ImageView bottomSheetSchematic; |
| 78 | + @BindView(R.id.custom_dialog_desc) |
| 79 | + TextView bottomSheetDesc; |
| 80 | + |
| 81 | + BottomSheetBehavior bottomSheetBehavior; |
| 82 | + GestureDetector gestureDetector; |
| 83 | + private SharedPreferences compassPreference; |
| 84 | + private float currentDegree = 0f; |
| 85 | + private int direction; // 0 for X-axis, 1 for Y-axis and 2 for Z-axis |
| 86 | + private SensorManager mSensorManager; |
| 87 | + |
| 88 | + @Override |
| 89 | + protected void onCreate(Bundle savedInstanceState) { |
| 90 | + super.onCreate(savedInstanceState); |
| 91 | + setContentView(R.layout.activity_compass_main); |
| 92 | + ButterKnife.bind(this); |
| 93 | + |
| 94 | + setSupportActionBar(mToolbar); |
| 95 | + |
| 96 | + compassPreference = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); |
| 97 | + setUpBottomSheet(); |
| 98 | + |
| 99 | + mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); |
| 100 | + xAxisRadioButton.setChecked(true); |
| 101 | + direction = 0; |
| 102 | + |
| 103 | + xAxisRadioButton.setOnClickListener(new View.OnClickListener() { |
| 104 | + @Override |
| 105 | + public void onClick(View v) { |
| 106 | + xAxisRadioButton.setChecked(true); |
| 107 | + yAxisRadioButton.setChecked(false); |
| 108 | + zAxisRadioButton.setChecked(false); |
| 109 | + direction = 0; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + yAxisRadioButton.setOnClickListener(new View.OnClickListener() { |
| 114 | + @Override |
| 115 | + public void onClick(View v) { |
| 116 | + xAxisRadioButton.setChecked(false); |
| 117 | + yAxisRadioButton.setChecked(true); |
| 118 | + zAxisRadioButton.setChecked(false); |
| 119 | + direction = 1; |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + zAxisRadioButton.setOnClickListener(new View.OnClickListener() { |
| 124 | + @Override |
| 125 | + public void onClick(View v) { |
| 126 | + xAxisRadioButton.setChecked(false); |
| 127 | + yAxisRadioButton.setChecked(false); |
| 128 | + zAxisRadioButton.setChecked(true); |
| 129 | + direction = 2; |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + protected void onResume() { |
| 136 | + super.onResume(); |
| 137 | + |
| 138 | + mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected void onPause() { |
| 143 | + super.onPause(); |
| 144 | + |
| 145 | + mSensorManager.unregisterListener(this); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void onSensorChanged(SensorEvent event) { |
| 150 | + |
| 151 | + float degree; |
| 152 | + switch (direction) { |
| 153 | + case 0: |
| 154 | + degree = Math.round(event.values[1]); |
| 155 | + break; |
| 156 | + case 1: |
| 157 | + degree = Math.round(event.values[2]); |
| 158 | + break; |
| 159 | + case 2: |
| 160 | + degree = Math.round(event.values[0]); |
| 161 | + break; |
| 162 | + default: |
| 163 | + degree = Math.round(event.values[1]); |
| 164 | + break; |
| 165 | + } |
| 166 | + |
| 167 | + setCompassAnimation(degree); |
| 168 | + |
| 169 | + degreeIndicator.setText(String.valueOf(degree)); |
| 170 | + currentDegree = -degree; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 175 | + // No use |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Sets rotational animation of compass image to provided degree angle |
| 180 | + * |
| 181 | + * @param degree Angle to which N-pole of compass should point |
| 182 | + */ |
| 183 | + |
| 184 | + private void setCompassAnimation(float degree) { |
| 185 | + |
| 186 | + RotateAnimation ra = new RotateAnimation( |
| 187 | + currentDegree, |
| 188 | + -degree, |
| 189 | + Animation.RELATIVE_TO_SELF, 0.5f, |
| 190 | + Animation.RELATIVE_TO_SELF, |
| 191 | + 0.5f); |
| 192 | + |
| 193 | + ra.setDuration(210); |
| 194 | + ra.setFillAfter(true); |
| 195 | + |
| 196 | + compass.startAnimation(ra); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 201 | + MenuInflater inflater = getMenuInflater(); |
| 202 | + inflater.inflate(R.menu.activity_compass_help_menu, menu); |
| 203 | + return true; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Initiates bottom sheet to display guide on how to use Compass instrument |
| 208 | + */ |
| 209 | + private void setUpBottomSheet() { |
| 210 | + |
| 211 | + bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); |
| 212 | + boolean isFirstTime = compassPreference.getBoolean("CompassFirstTime", true); |
| 213 | + |
| 214 | + if (isFirstTime) { |
| 215 | + bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); |
| 216 | + tvShadow.setAlpha(0.8f); |
| 217 | + arrowUpDown.setRotation(180); |
| 218 | + bottomSheetSlideText.setText(R.string.hide_guide_text); |
| 219 | + SharedPreferences.Editor editor = compassPreference.edit(); |
| 220 | + editor.putBoolean("CompassFirstTime", false); |
| 221 | + editor.apply(); |
| 222 | + } else { |
| 223 | + bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 224 | + } |
| 225 | + |
| 226 | + bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { |
| 227 | + private Handler handler = new Handler(); |
| 228 | + private Runnable runnable = new Runnable() { |
| 229 | + @Override |
| 230 | + public void run() { |
| 231 | + try { |
| 232 | + bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 233 | + } catch (IllegalArgumentException e) { |
| 234 | + bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); |
| 235 | + } |
| 236 | + } |
| 237 | + }; |
| 238 | + |
| 239 | + @Override |
| 240 | + public void onStateChanged(@NonNull final View bottomSheet, int newState) { |
| 241 | + switch (newState) { |
| 242 | + case BottomSheetBehavior.STATE_EXPANDED: |
| 243 | + handler.removeCallbacks(runnable); |
| 244 | + bottomSheetSlideText.setText(R.string.hide_guide_text); |
| 245 | + break; |
| 246 | + |
| 247 | + case BottomSheetBehavior.STATE_COLLAPSED: |
| 248 | + handler.postDelayed(runnable, 1000); |
| 249 | + break; |
| 250 | + |
| 251 | + default: |
| 252 | + handler.removeCallbacks(runnable); |
| 253 | + bottomSheetSlideText.setText(R.string.show_guide_text); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public void onSlide(@NonNull View bottomSheet, float slideOffset) { |
| 259 | + Float value = (float) MathUtils.map((double) slideOffset, 0.0, 1.0, |
| 260 | + 0.0, 0.8); |
| 261 | + tvShadow.setAlpha(value); |
| 262 | + arrowUpDown.setRotation(slideOffset * 180); |
| 263 | + } |
| 264 | + }); |
| 265 | + gestureDetector = new GestureDetector(this, |
| 266 | + new SwipeGestureDetector(bottomSheetBehavior)); |
| 267 | + } |
| 268 | + |
| 269 | + @Override |
| 270 | + public boolean onTouchEvent(MotionEvent event) { |
| 271 | + gestureDetector.onTouchEvent(event); |
| 272 | + return super.onTouchEvent(event); |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 277 | + switch (item.getItemId()) { |
| 278 | + case R.id.compass_help_icon: |
| 279 | + bottomSheetBehavior.setState(bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN ? |
| 280 | + BottomSheetBehavior.STATE_EXPANDED : BottomSheetBehavior.STATE_HIDDEN); |
| 281 | + break; |
| 282 | + default: |
| 283 | + break; |
| 284 | + } |
| 285 | + return true; |
| 286 | + } |
| 287 | +} |
0 commit comments