Android Open Source - SoundScheduler Main Activity






From Project

Back to project page SoundScheduler.

License

The source code is released under:

GNU General Public License

If you think the Android project SoundScheduler 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

/*
 * Sound Scheduler//from  w  w w  . j  a  v a2  s .com
 * Copyright (C) 2013 Victor Kifer
 */

package com.victorkifer.SoundScheduler;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.victorkifer.SoundScheduler.adapters.RuleListAdapter;
import com.victorkifer.SoundScheduler.database.RulesDataSource;
import com.victorkifer.SoundScheduler.entities.Rule;
import com.victorkifer.SoundScheduler.receivers.BootCompleteReceiver;
import com.victorkifer.SoundScheduler.receivers.TimeChangeReceiver;

import java.util.List;

public class MainActivity extends ActionBarActivity {
    private final String TAG = MainActivity.class.getSimpleName();
    private RulesDataSource rulesDS;

    public static final String EXTRA_RULE_ID = "ruleID";
    public static final String ACTION_RULELIST_UPDATE = "com.victorkifer.SoundScheduler.RULELIST_UPDATE";

    private List<Rule> rules;
    ListView rulesListView;
    RuleListAdapter rulesAdapter;

    private AdapterView.OnItemClickListener editRuleListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, EditRuleActivity.class);
            intent.putExtra(EXTRA_RULE_ID, id);
            startActivity(intent);
        }
    };

    private BroadcastReceiver ruleListUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            loadRules();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_RULELIST_UPDATE);
        registerReceiver(ruleListUpdateReceiver, intentFilter);

        rulesDS = new RulesDataSource(MainActivity.this);
        rulesDS.open();

        if(!TimeChangeReceiver.SERVICE_STARTED) {
            Log.d(TAG, "Creating receiver");

            BootCompleteReceiver.createAlarmBasedReceiver(getApplicationContext());
        }

        rulesListView = (ListView) findViewById(R.id.listView);
        TextView emptyText = (TextView)findViewById(android.R.id.empty);
        rulesListView.setEmptyView(emptyText);

        loadRules();
        rulesListView.setOnItemClickListener(editRuleListener);
    }

    private void loadRules() {
        rules = rulesDS.getAllRules();

        rulesAdapter = new RuleListAdapter(this, rules);
        rulesListView.setAdapter(rulesAdapter);
        rulesListView.setOnItemClickListener(editRuleListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case R.id.action_add:
                intent = new Intent(MainActivity.this, EditRuleActivity.class);
                startActivity(intent);
                break;
            case R.id.action_about:
                intent = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        rulesDS.close();
        unregisterReceiver(ruleListUpdateReceiver);

        super.onDestroy();
    }
}




Java Source Code List

com.victorkifer.SoundScheduler.AboutActivity.java
com.victorkifer.SoundScheduler.EditRuleActivity.java
com.victorkifer.SoundScheduler.MainActivity.java
com.victorkifer.SoundScheduler.adapters.RuleListAdapter.java
com.victorkifer.SoundScheduler.database.RulesDataSource.java
com.victorkifer.SoundScheduler.database.RulesDatabaseHelper.java
com.victorkifer.SoundScheduler.entities.Rule.java
com.victorkifer.SoundScheduler.listeners.RuleDeleteListener.java
com.victorkifer.SoundScheduler.listeners.RuleItemStateListener.java
com.victorkifer.SoundScheduler.managers.SoundManager.java
com.victorkifer.SoundScheduler.receivers.BootCompleteReceiver.java
com.victorkifer.SoundScheduler.receivers.TimeChangeReceiver.java