Android Open Source - Android-Counter-App Statistic Activity






From Project

Back to project page Android-Counter-App.

License

The source code is released under:

Apache License

If you think the Android project Android-Counter-App listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
  StatisticActivity/*from w ww  .  j  a v  a  2s.  c  om*/
  
  This is the second activity for the app.
  StatisticActivity is responsible for displaying counter stats.
  
  Copyright 2014 David Yee

  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

      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 ca.ualberta.cs.asn1;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/*
 * The StatisticActivity is a second activity used in this application.
 * It displays the count timestamps aggregated by counts per hour, 
 * counts per day, counts per week, and counts per month. The data does 
 * not require to be saved, but since a reset/zero counter button is 
 * available in this activity, we must use another CounterSaveModel to 
 * store any change (which in this case would be the count value of the 
 * counter) so that the CounterActivity can reflect the changes.
 */
public class StatisticActivity extends Activity {

  private ListView statList;
  protected List<String> statistics = new ArrayList<String>();
  protected ArrayAdapter<String> adapter;
  private CounterModel data;
  private StatisticModel processor;
  
  private CounterSaveModel resetData;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statistic_list_view);
    Bundle bundle = getIntent().getExtras();
      data = (CounterModel) bundle.getSerializable("CounterModel");
      
      resetData = new CounterSaveModel(StatisticActivity.this, this.getString(R.string.stat_sav));
      
      statList = (ListView) findViewById(R.id.statList);
    processor = new StatisticModel(data);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.statistic, menu);
    return true;
  }

  @Override
  protected void onResume() {
    super.onResume();
    
    /*
     * The following processing uses the helper methods found in the 
     * StatisticModel processor object to generate a string to add to 
     * the statistics list of strings.
     * 
     * Future improvements can be made to optimize the processing of the 
     * calendar data where nested for loops do not iterate through all 
     * possible cases. One optimization has been made where start and 
     * end year is determined thereby fixing the range of possible values
     * to iterate through.
     */
    
    statistics.clear();
    statistics.add(new String(this.getString(R.string.counts_per_month)));
    int start_year = processor.getStartYear();
    int end_year = processor.getEndYear();
    for(int j=start_year; j<=end_year; j++){
            for(int i=0; i<12; i++){
                int result = processor.findCountsPerMonth(i, j);
                if(result>0){
                    statistics.add(new String(processor.numberToMonth(i) + ": " + 
                                              String.valueOf(result)));
                }
            }
    }
    statistics.add(new String(this.getString(R.string.counts_per_week)));
    for(int j=start_year; j<=end_year; j++){
                    for(int i=0; i<12; i++){
                        for(int h=1; h<=5; h++){
                            int result = processor.findCountsPerWeek(h, i, j);
                            if(result>0){
                                statistics.add(new String("Week " + String.valueOf(h) + " of " + processor.numberToMonth(i) + ": " + 
                                                          String.valueOf(result)));
                            }
                        }
                    }
                }
    statistics.add(new String(this.getString(R.string.counts_per_day)));
    for(int j=start_year; j<=end_year; j++){
                    for(int i=0; i<12; i++){
                        for(int h=1; h<=31; h++){
                            int result = processor.findCountsPerDay(h, i, j);
                            if(result>0){
                                statistics.add(new String(processor.numberToMonth(i) + " " + 
                                                          String.valueOf(h) + ": " + 
                                                          String.valueOf(result)));
                            }
                        }
                    }
                
                }
    statistics.add(new String(this.getString(R.string.counts_per_hour)));
                for(int j=start_year; j<=end_year; j++){
                    for(int i=0; i<12; i++){
                        for(int h=1; h<=31; h++){
                            for(int g=0; g<24; g++){
                                int result = processor.findCountsPerHour(g, h, i, j);
                                if(result>0){
                                    String AMPM = new String();
                                    int time = g;
                                    if(time>12){
                                        AMPM = "PM";
                                        time-=12;
                                    }
                                    else{
                                        AMPM = "AM";
                                    }
                                    statistics.add(new String(processor.numberToMonth(i) + " " + 
                                                              String.valueOf(h) + " " + 
                                                              String.valueOf(time) + ":00" +AMPM + ": " + 
                                                              String.valueOf(result)));
                                }
                            }
                            
                        }
                    }
                }
    
    
    this.adapter = new ArrayAdapter<String>(this,
                R.layout.statistic_list, statistics);
    statList.setAdapter(adapter);

  }
  
  /*
   * Resets the counter. This function is assigned through a static XML file.
   * The counter has to be saved using the JSON save method defined in the 
   * CounterSaveModel. We save here after resetting so that the 
   * CounterActivity class can reflect these changes.
   */
  public void resetCounter(View view){
    data.setCount(0);
    ArrayList<CounterModel> out = new ArrayList<CounterModel>();
    out.add(data);
    resetData.saveData(out);
  }
  
}




Java Source Code List

ca.ualberta.cs.asn1.CounterActivity.java
ca.ualberta.cs.asn1.CounterListAdapter.java
ca.ualberta.cs.asn1.CounterModel.java
ca.ualberta.cs.asn1.CounterSaveModel.java
ca.ualberta.cs.asn1.StatisticActivity.java
ca.ualberta.cs.asn1.StatisticModel.java