com.trigger_context.conf.Action_Post_Tweet.java Source code

Java tutorial

Introduction

Here is the source code for com.trigger_context.conf.Action_Post_Tweet.java

Source

/*******************************************************************************
 *   Copyright 2013-2015 Karishma Sureka , Sai Gopal , Vijay Teja
 *
 *    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.trigger_context.conf;

import java.util.List;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.trigger_context.R;

public class Action_Post_Tweet extends Activity {

    private int mid = 1500;

    public Intent findTwitterClient() {
        final String[] twitterApps = { "com.twitter.android", // official - 10
                // 000
                "com.twidroid", // twidroid - 5 000
                "com.handmark.tweetcaster", // Tweetcaster - 5 000
                "com.thedeck.android" }; // TweetDeck - 5 000 ;
        Intent tweetIntent = new Intent();
        tweetIntent.setType("text/plain");
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent,
                PackageManager.MATCH_DEFAULT_ONLY);

        for (String twitterApp : twitterApps) {
            for (ResolveInfo resolveInfo : list) {
                String p = resolveInfo.activityInfo.packageName;
                if (p != null && p.startsWith(twitterApp)) {
                    tweetIntent.setPackage(p);
                    return tweetIntent;
                }
            }
        }
        return null;
    }

    public void noti(String title, String txt) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(txt);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mid++, mBuilder.build());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        String tweet = bundle.getString("tweetTextAction");

        Intent shareIntent = findTwitterClient();
        if (shareIntent != null) {
            shareIntent.putExtra(Intent.EXTRA_TEXT, tweet);
            startActivity(Intent.createChooser(shareIntent, "Share"));
            finish();
        } else {

            noti("Twitter action failed.", "No valid twitter app installed");
            finish();
        }
    }
}