package com.cameronlai.phonehaviour;

import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import java.util.List;

public class MainActivity extends ActionBarActivity {
    ListView mListView;
    Spinner mSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSpinner = (Spinner) findViewById(R.id.time_choice_spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.time_choice_arrays, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        mSpinner.setAdapter(adapter);

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // your code here
                String mString = (String) parentView.getItemAtPosition(position);
                if (mString == getString(R.string.day)) {
                    updateUsageStatistics(UsageStatsManager.INTERVAL_DAILY, 0, System.currentTimeMillis());
                } else if (mString == getString(R.string.week)) {
                    updateUsageStatistics(UsageStatsManager.INTERVAL_WEEKLY, 0, System.currentTimeMillis());
                } else if (mString == getString(R.string.month)) {
                    updateUsageStatistics(UsageStatsManager.INTERVAL_MONTHLY, 0, System.currentTimeMillis());
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }
        });
        // Confirm usage access
        // String tmpSettingStr = Settings.System.getString( getContentResolver(), Settings.ACTION_USAGE_ACCESS_SETTINGS  );
        // If not, prompt the user to set the usage access settings
    }

    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
    }

    @Override
    public void onPause() {
        super.onPause();  // Always call the superclass method first
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }
        else if(id == R.id.action_about)
        {
            Intent intent = new Intent(this, AboutActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void updateUsageStatistics(int timeSpan, long startTime, long endTime)
    {
        // Get usage statistics from Android OS
        final UsageStatsManager mUsageStatManager = (UsageStatsManager) getApplicationContext().getSystemService("usagestats");
        final List<UsageStats> stats = mUsageStatManager.queryUsageStats(timeSpan, startTime,  endTime);

        mListView = (ListView) findViewById(R.id.main_activity_all_package_name);
        ListEntryArrayAdapter adapter = new ListEntryArrayAdapter(this, stats, findViewById(R.id.main_activity_total_usage_textview));
        mListView.setAdapter(adapter);
    }
}

@@ -1677,527 +1677,39 @@
     
-if (mString == getString(R.string.day)) %7B%0A                    updateUsageStatistics(UsageStatsManager.INTERVAL_DAILY, 0, System.currentTimeMillis());%0A                %7D else if (mString == getString(R.string.week)) %7B%0A                    updateUsageStatistics(UsageStatsManager.INTERVAL_WEEKLY, 0, System.currentTimeMillis());%0A                %7D else if (mString == getString(R.string.month)) %7B%0A                    updateUsageStatistics(UsageStatsManager.INTERVAL_MONTHLY, 0, System.currentTimeMillis());%0A                %7D
+updateUsageStatistics(mString);
 %0A   
@@ -2212,24 +2212,127 @@
 method first
+%0A%0A        String mString = (String) mSpinner.getSelectedItem();%0A        updateUsageStatistics(mString);
 %0A    %7D%0A%0A    
@@ -3543,29 +3543,30 @@
     %7D%0A%0A    p
-ublic
+rivate
  void update
@@ -3581,19 +3581,22 @@
 tistics(
+Str
 in
-t
+g
  timeSpa
@@ -3600,93 +3600,21 @@
 Span
-, long startTime, long endTime)%0A    %7B%0A        // Get usage statistics from Android OS
+String)%0A    %7B
 %0A   
@@ -3661,16 +3661,32 @@
 anager =
+%0A               
  (UsageS
@@ -3766,105 +3766,816 @@
     
-final List%3CUsageStats%3E stats = mUsageStatManager.queryUsageStats(timeSpan, startTime,  endTime);%0A
+long startTime = 0;%0A        long endTime = System.currentTimeMillis();%0A%0A        // Get string%0A        final List%3CUsageStats%3E stats;%0A        if (timeSpanString == getString(R.string.day)) %7B%0A            stats = mUsageStatManager.queryUsageStats(%0A                    UsageStatsManager.INTERVAL_DAILY, startTime,  endTime);%0A        %7D else if (timeSpanString == getString(R.string.week)) %7B%0A             stats = mUsageStatManager.queryUsageStats(%0A                     UsageStatsManager.INTERVAL_WEEKLY, startTime,  endTime);%0A        %7D else if (timeSpanString == getString(R.string.month)) %7B%0A            stats = mUsageStatManager.queryUsageStats(%0A                    UsageStatsManager.INTERVAL_MONTHLY, startTime,  endTime);%0A        %7D%0A        else%0A        %7B%0A            return;%0A        %7D%0A%0A        // Update ListView
 %0A   

Add update statics to OnResume