tokyo.ryogo.dropkick.sns.twitter.TwitterWrapper.java Source code

Java tutorial

Introduction

Here is the source code for tokyo.ryogo.dropkick.sns.twitter.TwitterWrapper.java

Source

/*
 * Copyright (C) 2016 Ryogo Amamiya ( http://ryogo.tokyo/ )
 *
 * 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 tokyo.ryogo.dropkick.sns.twitter;

import android.content.Context;
import android.content.Intent;

import tokyo.ryogo.dropkick.R;
import tokyo.ryogo.dropkick.activities.TwitterOAuthActivity;
import tokyo.ryogo.dropkick.sns.ISns;
import tokyo.ryogo.dropkick.sns.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

/**
 * Twitter4J?
 * ISns?
 */
public class TwitterWrapper implements ISns {

    private String mTweetText;
    private String mLastErrorMessage;
    private Context mContext;
    private Twitter mTwitter;

    public final int TWITTER_STATUS_MAX_CHARACTERS = 140;

    // ????
    public void initialize(Context context) {
        mContext = context;
        mTwitter = null;
        mTweetText = null;
    }

    // ????
    public boolean isConfiguredAccount() {
        return DKTwitter.hasAccessToken(mContext);
    }

    // ??
    public void configureAccount() {

        //????????
        Intent intent = new Intent(mContext, TwitterOAuthActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //
        mContext.startActivity(intent);

    }

    // ?
    public void clearAccount() {
        DKTwitter.ClearAccessToken(mContext);
    }

    // ???
    public boolean setStatus(Status status) {

        // ???????
        if (status.getBody().isEmpty()) {
            mLastErrorMessage = mContext.getString(R.string.msg_tweet_empty);
            return false;
        }

        // ?
        String tweetText = formatStatus(status);

        // ??
        if (!tweetText.isEmpty()) {
            // 0

            if (tweetText.length() <= TWITTER_STATUS_MAX_CHARACTERS) {
                // 140?????
                mTweetText = tweetText;
                return true;
            } else {
                // 140?
                mLastErrorMessage = mContext.getString(R.string.msg_over_characters);
                return false;
            }
        }

        return false; //?????????
    }

    private String formatStatus(Status status) {

        // ?
        StringBuilder text = new StringBuilder(status.getBody());

        if (!status.getHashTag()[0].isEmpty()) {
            // ????????
            // ??????+?1????
            for (int i = 0; i < status.getHashTag().length; i++) {
                text.append(" ");
                text.append(status.getHashTag()[i]);
            }
        }

        //???
        return text.toString();

    }

    // ???
    public void clearStatus() {
        mTweetText = "";
    }

    // ?????
    public boolean updateStatus() {

        // Twitter???????????
        if (mTwitter == null) {
            mTwitter = DKTwitter.getTwitterInstance(mContext);
        }

        try {
            // 
            mTwitter.updateStatus(mTweetText);
            return true;
        } catch (TwitterException e) {
            // ?
            mLastErrorMessage = mContext.getString(R.string.msg_err_internal) + e.getMessage();
            return false;
        }

    }

    // ??
    public String getLastErrorMessage() {
        return mLastErrorMessage;
    }
}