Java tutorial
/* * Copyright (C) 2015 MummyDing * * This file is part of Leisure( <https://github.com/MummyDing/Leisure> ) * * Leisure is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Leisure is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Leisure. If not, see <http://www.gnu.org/licenses/>. */ package com.mummyding.app.leisure.ui.about; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.support.design.widget.Snackbar; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import com.mummyding.app.leisure.R; import com.mummyding.app.leisure.support.CONSTANT; import com.mummyding.app.leisure.support.HttpUtil; import com.mummyding.app.leisure.support.Settings; import com.mummyding.app.leisure.support.Utils; import com.mummyding.app.leisure.ui.setting.SettingsFragment; import com.mummyding.app.leisure.ui.support.SwipeBackActivity; import com.squareup.okhttp.Callback; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.io.IOException; public class AboutActivity extends SwipeBackActivity implements SensorEventListener { private Toolbar toolbar; private SensorManager mSensorManager; private boolean isShakeMode = false; private int mLang = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Language mLang = Utils.getCurrentLanguage(); if (mLang > -1) { Utils.changeLanguage(this, mLang); } //set Theme if (Settings.isNightMode) { this.setTheme(R.style.NightTheme); } else { this.setTheme(R.style.DayTheme); } setContentView(R.layout.activity_about); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle(getString(R.string.about)); getSupportActionBar().setDisplayHomeAsUpEnabled(true); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); getFragmentManager().beginTransaction().replace(R.id.framelayout, new AboutFragment()).commit(); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); isShakeMode = Settings.getInstance().getBoolean(Settings.SHAKE_TO_RETURN, true); } @Override protected void onStop() { mSensorManager.unregisterListener(this); super.onStop(); } @Override protected void onPause() { mSensorManager.unregisterListener(this); super.onPause(); } @Override public void onSensorChanged(SensorEvent event) { if (isShakeMode == false) { return; } float value[] = event.values; if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { if ((Math.abs(value[0]) + Math.abs(value[1]) + Math.abs(value[2])) > CONSTANT.shakeValue) { onBackPressed(); } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }