Android Open Source - AndroidFortunes Main Activity






From Project

Back to project page AndroidFortunes.

License

The source code is released under:

Apache License

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

package org.codechimp.androidfortunes;
//www.  j  a  v a 2  s.  c  o  m
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {

    private static final int SHOW_SETTINGS = 12;

    @InjectView(R.id.swipe_container)
    SwipeRefreshLayout swipeLayout;
    @InjectView(R.id.content)
    TextView contentTextView;

    Quote currentQuote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initializeUI();

        AlarmHelper.setDailyAlarm(this);
    }

    @Override
    protected void onStart() {
        super.onStart();

        swipeLayout.setRefreshing(true);
        new GetRandomQuoteTask().execute();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        initializeUI();
    }

    private void initializeUI() {
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(getResources().getColor(android.R.color.holo_blue_bright),
                getResources().getColor(android.R.color.holo_green_light),
                getResources().getColor(android.R.color.holo_orange_light),
                getResources().getColor(android.R.color.holo_red_light));

        if (currentQuote != null)
            contentTextView.setText(currentQuote.getContent());
    }

    @Override
    public void onRefresh() {
        new GetRandomQuoteTask().execute();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_towear:
                if (currentQuote != null) {

                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            NotifyHelper.sendMessageToWear(getBaseContext(), currentQuote.getContent());
                        }
                    };
                    new Thread(runnable).start();
                }
                return true;
            case R.id.action_settings:
                Intent i = new Intent(this, SettingsActivity.class);
                i.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
                        GeneralUserPreferencesFragment.class.getName());
                i.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true);

                startActivityForResult(i, SHOW_SETTINGS);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case SHOW_SETTINGS: {
                AlarmHelper.setDailyAlarm(this);
                break;
            }
        }
    }


    private class GetRandomQuoteTask extends AsyncTask<Void, Void, Quote> {
        @Override
        protected Quote doInBackground(Void... params) {
            Quote q;
            q = CloudyFortunesClient.getCloudyFortunesApiClient().randomQuote();
            return q;
        }

        @Override
        protected void onPostExecute(Quote result) {
            swipeLayout.setRefreshing(false);

            currentQuote = result;

            contentTextView.setText(currentQuote.getContent());
        }
    }
}




Java Source Code List

org.codechimp.androidfortunes.AlarmHelper.java
org.codechimp.androidfortunes.AlarmReceiver.java
org.codechimp.androidfortunes.CloudyFortunesApiInterface.java
org.codechimp.androidfortunes.CloudyFortunesClient.java
org.codechimp.androidfortunes.GeneralUserPreferencesFragment.java
org.codechimp.androidfortunes.MainActivity.java
org.codechimp.androidfortunes.MobileListenerService.java
org.codechimp.androidfortunes.NotifyHelper.java
org.codechimp.androidfortunes.OnBootReceiver.java
org.codechimp.androidfortunes.Quote.java
org.codechimp.androidfortunes.SettingsActivity.java
org.codechimp.androidfortunes.WearActivity.java
org.codechimp.androidfortunes.WearListenerService.java
org.codechimp.util.TimePreference.java
org.codechimp.util.WakeLock.java