Android Open Source - Kwitter Main Activity






From Project

Back to project page Kwitter.

License

The source code is released under:

Apache License

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

/*
* Copyright 2014 Vasya Drobushkov// w w w.  j ava  2  s . c o m
*
* 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 com.krossovochkin.kwitter.activities;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.krossovochkin.kwitter.BuildConfig;
import com.krossovochkin.kwitter.R;
import com.krossovochkin.kwitter.fragments.MainFragment;
import com.krossovochkin.kwitter.listeners.SendTweetListener;
import com.krossovochkin.kwitter.listeners.TwitterActionListener;
import com.krossovochkin.kwitter.tasks.SendReplyAsyncTask;
import com.krossovochkin.kwitter.tasks.SendTweetAsyncTask;
import com.krossovochkin.kwitter.toolbox.Settings;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class MainActivity extends ActionBarActivity implements TwitterActionListener, SendTweetListener {

    public static final String TAG = MainActivity.class.getSimpleName();

    public static final int NOTIFICATION_ID_SENDING_TWEET = 17;
    public static final int NOTIFICATION_ID_ERROR_SENDING_TWEET = 18;

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

    private Twitter mTwitter;

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

        init();

        showMainFragment();
    }

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

        mToggle.syncState();
    }

    private void showMainFragment() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, MainFragment.newInstance(mTwitter), MainFragment.TAG)
                .commit();
    }

    @Override
    public void sendTweetRequest(String tweet) {
        new SendTweetAsyncTask(mTwitter, tweet, this).execute();
    }

    @Override
    public void sendReplyRequest(Status statusToReply, String tweet) {
        new SendReplyAsyncTask(mTwitter, tweet, statusToReply.getId(), this).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) {
        // 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();
        if (id == R.id.action_settings) {
            showSettingsActivity();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showSettingsActivity() {
        startActivity(new Intent(this, SettingsActivity.class));
    }

    private void init() {
        initToolbar();
        initTwitter();
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                mToolbar,
                R.string.drawer_open,
                R.string.drawer_close);

        mDrawerLayout.setDrawerListener(mToggle);
        setSupportActionBar(mToolbar);
    }

    private void initTwitter() {
        boolean authDataExists = Settings.getBoolean(this, Settings.AUTH_DATA_EXISTS, false);

        if (authDataExists) {
            ConfigurationBuilder cb = new ConfigurationBuilder();

            cb.setDebugEnabled(BuildConfig.DEBUG)
                    .setOAuthConsumerKey(getString(R.string.twitter_consumer_key))
                    .setOAuthConsumerSecret(getString(R.string.twitter_consumer_secret))
                    .setOAuthAccessToken(Settings.getString(this, Settings.ACCESS_TOKEN_KEY))
                    .setOAuthAccessTokenSecret(Settings.getString(this, Settings.ACCESS_TOKEN_SECRET_KEY))
                    .setJSONStoreEnabled(true);


            TwitterFactory tf = new TwitterFactory(cb.build());
            mTwitter = tf.getInstance();
        } else {
            startActivity(new Intent(this, AuthActivity.class));
            finish();
        }
    }

    @Override
    public void onPreSendTweet() {
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.sending_tweet))
                .setSmallIcon(R.drawable.ic_launcher)
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID_SENDING_TWEET, notification);
    }

    @Override
    public void onPostSendTweet(boolean isTweetSent) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID_SENDING_TWEET);

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        if(!isTweetSent) {


            Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.error_sendgin_tweet))
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentIntent(pendingIntent)
                    .build();
            notificationManager.notify(NOTIFICATION_ID_SENDING_TWEET, notification);
        }
    }
}




Java Source Code List

com.krossovochkin.kwitter.activities.AuthActivity.java
com.krossovochkin.kwitter.activities.MainActivity.java
com.krossovochkin.kwitter.activities.SettingsActivity.java
com.krossovochkin.kwitter.adapters.TimelineAdapter.java
com.krossovochkin.kwitter.adapters.ViewPagerAdapter.java
com.krossovochkin.kwitter.fragments.BaseTimelineFragment.java
com.krossovochkin.kwitter.fragments.MainFragment.java
com.krossovochkin.kwitter.fragments.MentionsFragment.java
com.krossovochkin.kwitter.fragments.NavigationDrawerFragment.java
com.krossovochkin.kwitter.fragments.SendTweetFragment.java
com.krossovochkin.kwitter.fragments.SettingsFragment.java
com.krossovochkin.kwitter.fragments.TimelineFragment.java
com.krossovochkin.kwitter.fragments.ViewPagerFragment.java
com.krossovochkin.kwitter.listeners.FavoriteListener.java
com.krossovochkin.kwitter.listeners.GetTimelineListener.java
com.krossovochkin.kwitter.listeners.RetweetListener.java
com.krossovochkin.kwitter.listeners.SendTweetListener.java
com.krossovochkin.kwitter.listeners.TweetActionListener.java
com.krossovochkin.kwitter.listeners.TwitterActionListener.java
com.krossovochkin.kwitter.tasks.FavoriteAsyncTask.java
com.krossovochkin.kwitter.tasks.GetMentionsAsyncTask.java
com.krossovochkin.kwitter.tasks.GetTimelineAsyncTask.java
com.krossovochkin.kwitter.tasks.RetweetAsyncTask.java
com.krossovochkin.kwitter.tasks.SendReplyAsyncTask.java
com.krossovochkin.kwitter.tasks.SendTweetAsyncTask.java
com.krossovochkin.kwitter.toolbox.Constants.java
com.krossovochkin.kwitter.toolbox.FileManager.java
com.krossovochkin.kwitter.toolbox.KeyboardHelper.java
com.krossovochkin.kwitter.toolbox.Settings.java