/*
* Copyright (C) 2011-2015 asksven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*et
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.asksven.betterbatterystats;
/**
* @author sven
*
*/
import java.util.ArrayList;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.asksven.android.common.privateapiproxies.BatteryStatsProxy;
import com.asksven.android.common.privateapiproxies.HistoryItem;
import com.asksven.android.system.AndroidVersion;
import com.asksven.betterbatterystats.R;
import com.asksven.betterbatterystats.adapters.HistAdapter;
public class HistActivity extends ActionBarListActivity
{
/**
* The logging TAG
*/
private static final String TAG = "HistActivity";
/**
* a progess dialog to be used for long running tasks
*/
ProgressDialog m_progressDialog;
/**
* The ArrayAdpater for rendering the ListView
*/
private HistAdapter m_listViewAdapter;
/**
* @see android.app.Activity#onCreate(Bundle@SuppressWarnings("rawtypes")
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.label_series));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
}
/* Request updates at startup */
@Override
protected void onResume()
{
super.onResume();
new LoadStatData().execute(this);
}
/**
* Add menu items
*
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.serie_menu, menu);
return true;
}
/**
* Define menu action
*
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.refresh:
// Refresh
doRefresh();
break;
}
return false;
}
private void doRefresh()
{
// new LoadStatData().execute(this);
// Display the reference of the stat
// this.setListViewAdapter();
BatteryStatsProxy.getInstance(this).invalidate();
m_listViewAdapter.notifyDataSetChanged();
}
// @see http://code.google.com/p/makemachine/source/browse/trunk/android/examples/async_task/src/makemachine/android/examples/async/AsyncTaskExample.java
// for more details
private class LoadStatData extends AsyncTask<Context, Integer, HistAdapter>
{
@Override
protected HistAdapter doInBackground(Context... params)
{
//super.doInBackground(params);
m_listViewAdapter = new HistAdapter(HistActivity.this, getHistList());
//StatsActivity.this.setListAdapter(m_listViewAdapter);
// getStatList();
return m_listViewAdapter;
}
@Override
protected void onPostExecute(HistAdapter o)
{
super.onPostExecute(o);
// update hourglass
if (m_progressDialog != null)
{
m_progressDialog.hide();
m_progressDialog = null;
}
HistActivity.this.setListAdapter(o);
}
@Override
protected void onPreExecute()
{
// update hourglass
// @todo this code is only there because onItemSelected is called twice
if (m_progressDialog == null)
{
m_progressDialog = new ProgressDialog(HistActivity.this);
m_progressDialog.setMessage(getString(R.string.message_computing));
m_progressDialog.setIndeterminate(true);
m_progressDialog.setCancelable(false);
m_progressDialog.show();
}
}
}
/**
* Get the Stat to be displayed
*
* @return a List of StatElements sorted (descending)
*/
protected ArrayList<HistoryItem> getHistList()
{
if (AndroidVersion.isFroyo())
{
Snackbar
.make(findViewById(android.R.id.content), R.string.message_no_hist_froyo, Snackbar.LENGTH_LONG)
.show();
// Toast.makeText(this, getString(R.string.message_no_hist_froyo), Toast.LENGTH_SHORT).show();
}
ArrayList<HistoryItem> myRet = new ArrayList<HistoryItem>();
BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(this);
try
{
myRet = mStats.getHistory(this);
//mStats.dumpHistory(this);
}
catch (Exception e)
{
Log.e(TAG, "An error occured while retrieving history. No result");
}
return myRet;
}
}