Java tutorial
/* * Copyright (C) 2013 YojiokiSoft * * This program 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. * * This program 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 this * program. If not, see <http://www.gnu.org/licenses/>. */ package com.yojiokisoft.yumekanow.activity; import java.sql.SQLException; import java.util.List; import android.content.Intent; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Vibrator; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.util.DisplayMetrics; import android.util.Pair; import android.view.Gravity; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.TextView; import com.googlecode.androidannotations.annotations.AfterViews; import com.googlecode.androidannotations.annotations.EActivity; import com.googlecode.androidannotations.annotations.Extra; import com.googlecode.androidannotations.annotations.OptionsItem; import com.googlecode.androidannotations.annotations.OptionsMenu; import com.googlecode.androidannotations.annotations.ViewById; import com.yojiokisoft.yumekanow.R; import com.yojiokisoft.yumekanow.adapter.MainPagerAdapter; import com.yojiokisoft.yumekanow.db.CardDao; import com.yojiokisoft.yumekanow.db.CounterDao; import com.yojiokisoft.yumekanow.db.SettingDao; import com.yojiokisoft.yumekanow.entity.CardEntity; import com.yojiokisoft.yumekanow.exception.MyUncaughtExceptionHandler; import com.yojiokisoft.yumekanow.utils.MyAlarmManager; import com.yojiokisoft.yumekanow.utils.MyConst; import com.yojiokisoft.yumekanow.utils.MyImage; import com.yojiokisoft.yumekanow.utils.MyResource; /** * */ @EActivity(R.layout.activity_main) @OptionsMenu(R.menu.activity_main) public class MainActivity extends FragmentActivity { @ViewById(android.R.id.tabhost) /*package*/TabHost mTabHost; @ViewById(R.id.pager) /*package*/ViewPager mPager; @Extra(MyConst.EN_FIRE_EVENT) /*package*/String mFireEvent; private Vibrator mVibrator = null; private Ringtone mRingtone = null; private boolean mCloseByButtonClicked; // ????? /** * ?? (onCreate????? */ @AfterViews /*package*/void initActivity() { // ????????? Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); SettingDao settingDao = SettingDao.getInstance(); // px -> dp ? if (!settingDao.getConvDpFlag()) { convPx2Dp(); settingDao.setConvDpFlag(true); } if (MyAlarmManager.getStartTime() == 0 && MyAlarmManager.getWakeUpTime() == 0) { MyAlarmManager.setStartTimer(this); } FragmentManager manager = getSupportFragmentManager(); mTabHost.setup(); final MainPagerAdapter adapter = new MainPagerAdapter(manager, this, mTabHost, mPager); // tab size from screen size DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int tabWidth = metrics.widthPixels / 3; int tabHeight = metrics.widthPixels / 7; String tabTitle[] = new String[3]; tabTitle[0] = getString(R.string.tab_title1); tabTitle[1] = getString(R.string.tab_title2); tabTitle[2] = getString(R.string.tab_title3); for (int i = 0; i < tabTitle.length; i++) { TabSpec spec = mTabHost.newTabSpec("tab" + i); spec.setIndicator(getTabView(tabWidth, tabHeight, tabTitle[i])); adapter.addTab(spec, "tab" + i); } if ("Timer".equals(mFireEvent)) { if (settingDao.getVibrator()) { mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); long[] pattern = { 0, 500, 250, 500, 250, 500 }; // OFF/ON/OFF/ON... mVibrator.vibrate(pattern, -1); } String url = settingDao.getAlarmUrl(); if (url != null) { mRingtone = RingtoneManager.getRingtone(this, Uri.parse(url)); mRingtone.play(); } } mCloseByButtonClicked = false; } /** * ? */ @Override protected void onStart() { super.onStart(); MyUncaughtExceptionHandler.sendBugReport(this); } /** * ? */ @Override protected void onDestroy() { super.onDestroy(); stopVibrator(); if (!mCloseByButtonClicked) { CounterDao counterDao; try { counterDao = new CounterDao(); counterDao.setCounter(false); } catch (SQLException e) { MyUncaughtExceptionHandler.sendBugReport(this, e); } } } /** * ????????. * * @param width * @param height * @param title * @return */ private TextView getTabView(int width, int height, String title) { TextView view = new TextView(this); view.setMinimumWidth(width); view.setMinimumHeight(height); view.setGravity(Gravity.CENTER); view.setBackgroundResource(R.drawable.tab_indicator_holo); view.setText(title); return view; } /** * ????. */ @OptionsItem(R.id.make_card) /*package*/void onMenuMakeCard() { Intent myIntent = new Intent(getApplicationContext(), MakeCardActivity_.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(myIntent); } /** * ??????. */ @OptionsItem(R.id.select_card) /*package*/void onMenuSelectCard() { Intent myIntent = new Intent(getApplicationContext(), CardListActivity_.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(myIntent); } /** * ????. */ @OptionsItem(R.id.settings) /*package*/void onMenuSettings() { Intent myIntent = new Intent(getApplicationContext(), MyPreference_.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(myIntent); } /** * ?????. */ @OptionsItem(R.id.usage) /*package*/void onMenuUsage() { Intent myIntent = new Intent(getApplicationContext(), UsageActivity_.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(myIntent); } /** * ????? */ public void stopVibrator() { if (mVibrator != null) { mVibrator.cancel(); mVibrator = null; } if (mRingtone != null) { mRingtone.stop(); mRingtone = null; } } /** * ? */ public void closeActivity() { mCloseByButtonClicked = true; finish(); } /** * px -> dp ? */ private void convPx2Dp() { Pair<Integer, Integer> wh = MyImage.getScreenWidthAndHeight(this); int imgH = MyResource.px2Dip(wh.second - (MyResource.getStatusBarHeight() * 2) - (wh.first / 7)); int imgW = MyResource.px2Dip(wh.first); float vRatio = imgH / 656.0f; float hRatio = imgW / 480.0f; try { CardDao cardDao = new CardDao(); List<CardEntity> list = cardDao.queryForAll(); for (CardEntity card : list) { card.marginLeft = (int) (card.marginLeft * hRatio + 0.5f); card.marginTop = (int) (card.marginTop * vRatio + 0.5f); cardDao.createOrUpdate(card); } } catch (SQLException e) { MyUncaughtExceptionHandler.sendBugReport(this, e); } } }