Back to project page android_device.
The source code is released under:
[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...
If you think the Android project android_device listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * ================================================================================================= * Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at/*from ww w.ja va 2 s .c o m*/ * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.wit.android.device.examples.fragment; import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.view.View; import android.widget.ListView; import android.widget.TextView; import com.wit.android.device.examples.R; import com.wit.android.device.examples.adapter.BatteryInfoAdapter; import com.wit.android.device.examples.model.BatteryInfo; import com.wit.android.device.Battery; import com.wit.android.examples.libs.fragment.annotation.ExActionBarOptions; import com.wit.android.examples.libs.fragment.annotation.ExContentView; /** * <p> * Description. * </p> * * @author Martin Albedinsky */ @ExContentView(R.layout.fragment_battery_info) @ExActionBarOptions(title = R.string.Navigation_Label_BatteryInfo) public class BatteryInfoFragment extends BaseDeviceFragment { /** * Log TAG. */ // private static final String TAG = BatteryInfoFragment.class.getSimpleName(); /** * */ private final BatteryListener BATTERY_LISTENER = new BatteryListener(); /** * */ private BatteryInfoAdapter mAdapter; /** * */ private TextView mTechnology, mVoltage, mTemperature; /** * */ private boolean bRegisterStatusReceiver = true; /** * */ private boolean bRegisterHealthReceiver = false; /** * */ private boolean bRegisterPluggedReceiver = false; /** */ @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); this.mTechnology = (TextView) view.findViewById(R.id.fragment_battery_info_text_view_technology); this.mVoltage = (TextView) view.findViewById(R.id.fragment_battery_info_text_view_voltage); this.mTemperature = (TextView) view.findViewById(R.id.fragment_battery_info_text_view_temperature); // Set up list view and adapter. if (mAdapter == null) { mAdapter = new BatteryInfoAdapter(getActivity()); } ((ListView) view.findViewById(android.R.id.list)).setAdapter(mAdapter); final Battery battery = getAndroidDevice().getBattery(); // Register battery global callback. battery.registerOnBatteryListener(BATTERY_LISTENER); // Register all battery broadcast receivers to handle all battery updates. final Context context = getActivity(); if (bRegisterStatusReceiver) { battery.registerBatteryReceiver(context, Battery.RECEIVER_BATTERY_STATUS); } if (bRegisterHealthReceiver) { battery.registerBatteryReceiver(context, Battery.RECEIVER_BATTERY_HEALTH); } if (bRegisterPluggedReceiver) { battery.registerBatteryReceiver(context, Battery.RECEIVER_BATTERY_PLUGGED_STATE); } } /** */ @Override public void onDestroyView() { super.onDestroyView(); final Battery battery = getAndroidDevice().getBattery(); // UnRegister battery global callback. battery.unregisterOnBatteryListener(BATTERY_LISTENER); // UnRegister all battery broadcast receivers. final Context context = getActivity(); if (bRegisterStatusReceiver) { battery.unregisterBatteryReceiver(context, Battery.RECEIVER_BATTERY_STATUS); } if (bRegisterHealthReceiver) { battery.unregisterBatteryReceiver(context, Battery.RECEIVER_BATTERY_HEALTH); } if (bRegisterPluggedReceiver) { battery.unregisterBatteryReceiver(context, Battery.RECEIVER_BATTERY_PLUGGED_STATE); } } /** * @param battery */ private void dispatchBatteryStatusChange(Battery battery) { if (mAdapter != null) { mAdapter.dispatchNewInfo(new BatteryInfo(battery)); } // Update "static" info. // Note that technology isn't updated, it remains same // so it shouldn't be updated each time. mTechnology.setText(battery.getTechnology().originalName); mVoltage.setText(getString( R.string.Battery_Info_Label_Voltage_format, battery.getVoltage() )); mTemperature.setText(getString( R.string.Battery_Info_Label_Temperature_format, battery.getTemperature() )); } /** * @param plugged */ private void dispatchBatteryPluggedStateChange(boolean plugged) { if (mAdapter != null) { final BatteryInfo info = new BatteryInfo(); info.setChange(plugged ? getString(R.string.Battery_Info_Change_PluggedState_plugged) : getString(R.string.Battery_Info_Change_PluggedState_unplugged) ); mAdapter.dispatchNewInfo(info); } } /** * @param low */ private void dispatchBatteryHealthChange(boolean low) { if (mAdapter != null) { final BatteryInfo info = new BatteryInfo(); info.setChange(low ? getString(R.string.Battery_Info_Change_Health_OkLow) : getString(R.string.Battery_Info_Change_Health_LowOk) ); mAdapter.dispatchNewInfo(info); } } /** * */ private class BatteryListener extends Battery.SimpleListener { /** */ @Override public void onPluggedStateChange(@NonNull Battery battery, boolean plugged, @NonNull Context context) { dispatchBatteryPluggedStateChange(plugged); } @Override public void onHealthChange(@NonNull Battery battery, boolean isLow, @NonNull Context context) { dispatchBatteryHealthChange(isLow); } /** */ @Override public void onStatusChange(@NonNull Battery battery, @NonNull Context context) { dispatchBatteryStatusChange(battery); } } }