/* Swisscom Safe Connect Copyright (C) 2015 Swisscom This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.swisscom.safeconnect.sharing; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import android.content.Context; import android.content.Intent; import android.util.Log; import com.swisscom.safeconnect.BuildConfig; import com.swisscom.safeconnect.R; public class TwitterSharing extends Sharing { public final static String PACKAGE_NAME = "com.twitter.android"; private final static String API_URL = "https://twitter.com/intent/tweet?"; private final static String URL_CODE = "url="; private final static String TEXT_CODE = "text="; private final static String HASHTAG = " #SafeConnect "; public TwitterSharing(Context ctx){ super(ctx, PACKAGE_NAME); } @Override protected Intent shareWithBrowser(String text, String url) { return getBrowserIntent(API_URL+TEXT_CODE+text+"&"+URL_CODE+url, "Twitter", R.drawable.twitter); } @Override public Intent getIntent(String text, String url) { String twitterContent = getTwitterText(text, url); Intent intent = getSharingApp(PACKAGE_NAME, "", twitterContent+url, mContext); if (intent == null) { // need to url encode this stuff for Twitter try { twitterContent = URLEncoder.encode(twitterContent, "UTF-8"); url = URLEncoder.encode(url, "UTF-8"); } catch (UnsupportedEncodingException e) { if (BuildConfig.DEBUG) Log.e("SafeApp", "Unsupported encoding", e); } intent = shareWithBrowser(twitterContent, url); } return intent; } private String getTwitterText(String text, String url){ String twitterContent = text; //HTTPS url is counted as 23 chars //https://dev.twitter.com/docs/faq#5810 final int urlCharsToCount = 23; if ((text.length() + HASHTAG.length() + urlCharsToCount > 140)) { twitterContent = text.substring(0, (136 - HASHTAG.length() - urlCharsToCount)) + "..."; } return twitterContent + HASHTAG; } }