Java tutorial
/* IRKitSettingActivity.java Copyright (c) 2014 NTT DOCOMO,INC. Released under the MIT license http://opensource.org/licenses/mit-license.php */ package org.deviceconnect.android.deviceplugin.irkit.settings.activity; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import org.deviceconnect.android.deviceplugin.irkit.BuildConfig; import org.deviceconnect.android.deviceplugin.irkit.IRKitManager; import org.deviceconnect.android.deviceplugin.irkit.IRKitManager.WiFiSecurityType; import org.deviceconnect.android.deviceplugin.irkit.R; import org.deviceconnect.android.deviceplugin.irkit.settings.fragment.IRKitAccessPointSettingFragment; import org.deviceconnect.android.deviceplugin.irkit.settings.fragment.IRKitBaseFragment; import org.deviceconnect.android.deviceplugin.irkit.settings.fragment.IRKitEndingFragment; import org.deviceconnect.android.deviceplugin.irkit.settings.fragment.IRKitPowerOnFragment; import org.deviceconnect.android.deviceplugin.irkit.settings.fragment.IRKitWiFiSelectionFragment; import org.deviceconnect.android.deviceplugin.irkit.settings.widget.HoldableViewPager; import java.util.List; /** * Activity. * @author NTT DOCOMO, INC. */ public class IRKitSettingActivity extends IRKitAbstractSettingActivity { /** . */ private static final int PAGE_COUNT = 4; /** * SharedPreference ???. */ public static final String SP_NAME = "irkit"; /** * SharedPreference clientkey. */ public static final String SP_KEY_CLIENT_KEY = "client_key"; /** * . */ private String mClientKey; /** * ID. */ private String mServiceId; /** * ?. */ private String mDeviceKey; /** * ??. */ private int mPrePage; /** * SSID. */ private String mSSID; /** * . */ private String mPassword; /** * . */ private WiFiSecurityType mSecType; /** * ?. */ @SuppressWarnings({ "rawtypes" }) private static final Class[] PAGES = { IRKitPowerOnFragment.class, IRKitAccessPointSettingFragment.class, IRKitWiFiSelectionFragment.class, IRKitEndingFragment.class, }; private static final int[] PAGE_TITLES = { R.string.page01, R.string.page02, R.string.page03, R.string.page04 }; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (null == savedInstanceState) { mPrePage = 0; mSecType = WiFiSecurityType.WPA2; getViewPager().addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(final int position) { Fragment next = getCurrentFragment(position); Fragment pre = getCurrentFragment(mPrePage); if (next != null && next instanceof IRKitBaseFragment) { ((IRKitBaseFragment) next).onAppear(); } if (pre != null && pre instanceof IRKitBaseFragment) { ((IRKitBaseFragment) pre).onDisapper(); } mPrePage = position; } @Override public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(final int state) { } }); IRKitManager.INSTANCE.init(this); final SharedPreferences sp = getSharedPreferences(SP_NAME, MODE_PRIVATE); mClientKey = sp.getString(SP_KEY_CLIENT_KEY, null); } } @Override public Fragment createPage(final int position) { Fragment page; try { page = (Fragment) PAGES[position].newInstance(); } catch (InstantiationException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } page = null; } catch (IllegalAccessException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } page = null; } return page; } @Override public int getPageCount() { return PAGE_COUNT; } /** * ??. * * @return ???null? */ public String getClientKey() { return mClientKey; } /** * ??. * * @param clientKey */ public void saveClientKey(final String clientKey) { SharedPreferences sp = getSharedPreferences(SP_NAME, MODE_PRIVATE); sp.edit().putString(SP_KEY_CLIENT_KEY, clientKey).commit(); } @Override protected void onResume() { super.onResume(); Fragment f = getCurrentFragment(getViewPager().getCurrentItem()); if (f != null && f instanceof IRKitBaseFragment) { ((IRKitBaseFragment) f).onEnterForeground(); } } @Override protected void onPause() { super.onPause(); Fragment f = getCurrentFragment(getViewPager().getCurrentItem()); if (f != null && f instanceof IRKitBaseFragment) { ((IRKitBaseFragment) f).onEnterBackground(); } } @Override public void onConfigurationChanged(final Configuration newConfig) { super.onConfigurationChanged(newConfig); } /** * getFragments()?getViewPager().getCurrentItem()???? * ????Fragment? * @return ????Fragment */ private Fragment getCurrentFragment(final int current) { FragmentManager fm = getSupportFragmentManager(); Fragment f = null; List<Fragment> list = fm.getFragments(); if (list.size() == 0) { return f; } String className = PAGES[current].getSimpleName(); for (Fragment fragment : list) { if (fragment.getClass().getSimpleName().equals(className)) { f = fragment; } } return f; } /** * ID?. * * @param serviceId ID */ public void setServiceId(final String serviceId) { mServiceId = serviceId; } /** * ??. * * @param deviceKey ? */ public void setDeviceKey(final String deviceKey) { mDeviceKey = deviceKey; } /** * ID??. * * @return ID */ public String getServiceId() { return mServiceId; } /** * ???. * * @return ? */ public String getDeviceKey() { return mDeviceKey; } /** * ??????. * * @param touchEnable ??? */ public void setTouchEnable(final boolean touchEnable) { ((HoldableViewPager) getViewPager()).setScrollable(touchEnable); } /** * SSID?. * * @param ssid SSID */ public void setSSID(final String ssid) { mSSID = ssid; } /** * ?. * * @param password */ public void setPassword(final String password) { mPassword = password; } /** * ?. * * @param type */ public void setSecType(final WiFiSecurityType type) { mSecType = type; } /** * SSID??. * * @return SSID */ public String getSSID() { return mSSID; } /** * ??. * * @return */ public String getPassword() { return mPassword; } /** * ??. * * @return */ public WiFiSecurityType getSecType() { return mSecType; } }