/*
* Copyright 2010-2011 Android DEvelopers' cluB
*
* 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.
*
* $Id: KushikatsuHelper.java 147 2011-02-15 23:18:37Z makoto1975 $
*/
package jp.andeb.kushikatsu.helper;
import static android.app.Activity.RESULT_FIRST_USER;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
/**
* {@code KuShiKaTsu}
*
* <p>
*
* </p>
*/
public final class KushikatsuHelper {
/**
* {@code KuShiKaTsu}
*/
public static final String PACKAGE_NAME = "jp.andeb.kushikatsu";
/**
* {@link Intent}
*/
public static final class SendIntent {
/**
* {@link Intent} Push {@code Intent} {@code ACTION}
*/
public static final String ACTION = PACKAGE_NAME + ".FELICA_INTENT";
/**
* {@link Intent} Push {@code Intent} {@code CATEGORY}
*/
public static final String CATEGORY = Intent.CATEGORY_DEFAULT;
/**
* {@link Intent}
*
* <p>
*
* </p>
*/
public static final String EXTRA_INTENT = "EXTRA_INTENT";
}
/**
*
*/
public static final class StartBrowser {
/**
* Push {@code Intent} {@code ACTION}
*/
public static final String ACTION = PACKAGE_NAME + ".FELICA_BROWSER";
/**
* Push {@code Intent} {@code CATEGORY}
*/
public static final String CATEGORY = Intent.CATEGORY_DEFAULT;
/**
* URL({@link String})
*
* <p>
*
* </p>
*/
public static final String EXTRA_URL = "EXTRA_URL";
/**
* ({@link String})
*
* <p>
*
* </p>
*/
public static final String EXTRA_BROWSER_PARAM = "EXTRA_BROWSER_PARAM";
}
/**
*
*/
public static final class StartMailer {
/**
* Push {@code Intent} {@code ACTION}
*/
public static final String ACTION = PACKAGE_NAME + ".FELICA_MAILER";
/**
* Push {@code Intent} {@code CATEGORY}
*/
public static final String CATEGORY = Intent.CATEGORY_DEFAULT;
/**
* To({@link String}{@code []})
*
* <p>
*
* </p>
*/
public static final String EXTRA_EMAIL = "EXTRA_EMAIL";
/**
* Cc({@link String}{@code []})
*
* <p>
*
* </p>
*/
public static final String EXTRA_CC = "EXTRA_CC";
/**
* Subject({@link String})
*
* <p>
*
* </p>
*/
public static final String EXTRA_SUBJECT = "EXTRA_SUBJECT";
/**
* ({@link String})
*
* <p>
*
* </p>
*/
public static final String EXTRA_TEXT = "EXTRA_TEXT";
/**
* ({@link String})
*
* <p>
*
* </p>
*/
public static final String EXTRA_MAIL_PARAM = "EXTRA_MAIL_PARAM";
}
/**
*
*/
public static final class CommonParam {
/**
* ({@code int})
*/
public static final String EXTRA_SEND_TIMEOUT = "EXTRA_SEND_TIMEOUT";
/**
*
*
* <ul>
* <li>({@code String}): </li>
* <li>KuShiKaTsu ({@code String}): KuShiKaTsu </li>
* <li>ID({@code int}: </li>
* </ul>
*
* <p>
*
* </p>
* <p>
*
* </p>
*/
public static final String EXTRA_SOUND_ON_SENT = "EXTRA_SOUND_ON_SENT";
}
/*
* result code RESULT_OK RESULT_CANCELED android
*
*/
/**
* Push ({@code =}{@value #RESULT_UNEXPECTED_ERROR})
*/
public static final int RESULT_OK = Activity.RESULT_OK;
/**
* Push ({@code =}{@value #RESULT_UNEXPECTED_ERROR})
*/
public static final int RESULT_CANCELED = Activity.RESULT_CANCELED;
/**
*
* ({@code =}{@value #RESULT_UNEXPECTED_ERROR})
*/
public static final int RESULT_UNEXPECTED_ERROR = RESULT_FIRST_USER + 0;
/**
* Activity {@link Intent}
* ({@code =}{@value #RESULT_INVALID_EXTRA})
*/
public static final int RESULT_INVALID_EXTRA = RESULT_FIRST_USER + 1;
/**
* FeliCa
* ({@code =}{@value #RESULT_DEVICE_NOT_FOUND})
*/
public static final int RESULT_DEVICE_NOT_FOUND = RESULT_FIRST_USER + 2;
/**
*
* ({@code =}{@value #RESULT_DEVICE_IN_USE})
*/
public static final int RESULT_DEVICE_IN_USE = RESULT_FIRST_USER + 3;
/**
* {@link Intent} URL {@code FeliCa Push}
* ({@code =}{@value #RESULT_TOO_BIG})
*/
public static final int RESULT_TOO_BIG = RESULT_FIRST_USER + 4;
/**
*
* ({@code =}{@value #RESULT_TIMEOUT})
*/
public static final int RESULT_TIMEOUT = RESULT_FIRST_USER + 5;
/**
* FeliCa
* ({@code =}{@value #RESULT_NOT_INITIALIZED})
*/
public static final int RESULT_NOT_INITIALIZED = RESULT_FIRST_USER + 6;
/**
* FeliCa
* ({@code =}{@value #RESULT_DEVICE_LOCKED})
*/
public static final int RESULT_DEVICE_LOCKED = RESULT_FIRST_USER + 7;
/**
* Push
* ({@code =}{@value #RESULT_PUSH_REGISTERED})
* Gingerbread NFC API Push
* Push OS {@link Intent}
*
*/
public static final int RESULT_PUSH_REGISTERED = RESULT_FIRST_USER + 8;
/**
* KuShiKaTsu ({@value #SOUND_1})
*/
public static final String SOUND_1 = "se1";
/**
* KuShiKaTsu ({@value #SOUND_2})
*/
public static final String SOUND_2 = "se2";
/**
* KuShiKaTsu ({@value #SOUND_3})
*/
public static final String SOUND_3 = "se3";
private KushikatsuHelper() {
throw new AssertionError("instatiation prohibited.");
}
/**
* KuShiKaTsu
*
* <p>
* {@code ACTION}
* {@value jp.andeb.kushikatsu.helper.KushikatsuHelper.SendIntent#ACTION}
* {@link Intent}
* </p>
*
* @param context
* {@code null}
* @return
* {@code true} {@code false}
*
* @throws IllegalArgumentException
* {@code null} {@code null}
*/
public static boolean isKushikatsuInstalled(final Context context) {
if (context == null) {
throw new IllegalArgumentException("'context' must not be null.");
}
final PackageManager pm = context.getPackageManager();
final Intent intent = new Intent(SendIntent.ACTION);
final List<ResolveInfo> resolveInfo = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
final boolean result = (!resolveInfo.isEmpty());
return result;
}
/**
* KuShiKaTsu {@link Intent}
*/
public static Intent buildIntentForKushikatsuInstall() {
final Uri uri = Uri.parse("market://details?id=" + PACKAGE_NAME);
final Intent i = new Intent(Intent.ACTION_VIEW, uri);
return i;
}
/**
* {@link Intent} {@link Intent}
*
* @param remoteIntent
* {@link Intent}{@code null}
* @return
* KuShiKaTsu {@link Intent} {@link Intent}
* @throws IllegalArgumentException
* {@code null} {@code null}
*/
public static Intent buildIntentForSendIntent(Intent remoteIntent) {
if (remoteIntent == null) {
throw new IllegalArgumentException(
"'remoteIntent' must not be null.");
}
final Intent intent = new Intent(SendIntent.ACTION);
intent.addCategory(SendIntent.CATEGORY);
intent.putExtra(SendIntent.EXTRA_INTENT, remoteIntent);
return intent;
}
/**
* {@link Intent}
*
* @param url
* URL {@code null}
* @param browserParam
*
* {@code null}
* @return
* KuShiKaTsu {@link Intent}
* @throws IllegalArgumentException
* {@code null} {@code null}
*/
public static Intent buildIntentForStartBrowser(String url,
String browserParam) {
if (url == null) {
throw new IllegalArgumentException("'url' must not be null.");
}
final Intent intent = new Intent(StartBrowser.ACTION);
intent.addCategory(StartBrowser.CATEGORY);
intent.putExtra(StartBrowser.EXTRA_URL, url);
if (browserParam != null) {
intent.putExtra(StartBrowser.EXTRA_BROWSER_PARAM, browserParam);
}
return intent;
}
/**
* {@link Intent}
*
* @param to
*
* {@code null}
* @param cc
* CC
* CC {@code null}
* @param subject
*
* {@code null}
* @param body
*
* {@code null}
* @param mailerParam
*
* {@code null}
* @return
* KuShiKaTsu {@link Intent}
*/
public static Intent buildIntentForStartMailer(String[] to, String[] cc,
String subject, String body, String mailerParam) {
final Intent intent = new Intent(StartMailer.ACTION);
intent.addCategory(StartMailer.CATEGORY);
if (to != null) {
intent.putExtra(StartMailer.EXTRA_EMAIL, to);
}
if (cc != null) {
intent.putExtra(StartMailer.EXTRA_CC, cc);
}
if (subject != null) {
intent.putExtra(StartMailer.EXTRA_SUBJECT, subject);
}
if (body != null) {
intent.putExtra(StartMailer.EXTRA_TEXT, body);
}
if (mailerParam != null) {
intent.putExtra(StartMailer.EXTRA_MAIL_PARAM, mailerParam);
}
return intent;
}
/**
* KuShiKaTsu Push
*
* @param intent
* {@link #buildIntentForSendIntent(Intent)}
* {@link #buildIntentForStartBrowser(String, String)}
* {@link #buildIntentForStartMailer(String[], String[], String, String, String)}
* {@link Intent} {@code null}
* @param timeoutSec
* {@code 1}
* @throws IllegalArgumentException
*
* <ul>
* <li>{@code null} {@code null} </li>
* <li> {@code 0} </li>
* </ul>
*/
public static void setSendTimeout(Intent intent, int timeoutSec) {
if (intent == null) {
throw new IllegalArgumentException("'intent' must not be null.");
}
if (timeoutSec <= 0) {
throw new IllegalArgumentException("'timeoutSec' must be positive.");
}
intent.putExtra(CommonParam.EXTRA_SEND_TIMEOUT, timeoutSec);
}
/**
* KuShiKaTsu Push
*
* @param intent
* {@link #buildIntentForSendIntent(Intent)}
* {@link #buildIntentForStartBrowser(String, String)}
* {@link #buildIntentForStartMailer(String[], String[], String, String, String)}
* {@link Intent} {@code null}
* @param soundName
* KuShiKaTsu {@link #SOUND_1} {@link #SOUND_2}
* {@link #SOUND_3} {@code null}
* @throws IllegalArgumentException
* {@code null} {@code null}
*/
public static void setSoundOnSent(Intent intent, String soundName) {
if (intent == null) {
throw new IllegalArgumentException("'intent' must not be null.");
}
if (soundName == null) {
throw new IllegalArgumentException("'soundName' must not be null.");
}
intent.putExtra(CommonParam.EXTRA_SOUND_ON_SENT, soundName);
}
/**
* KuShiKaTsu Push
*
* @param intent
* {@link #buildIntentForSendIntent(Intent)}
* {@link #buildIntentForStartBrowser(String, String)}
* {@link #buildIntentForStartMailer(String[], String[], String, String, String)}
* {@link Intent} {@code null}
* @param soundResId
*
* @throws IllegalArgumentException
* {@code null} {@code null}
*/
public static void setSoundOnSent(Intent intent, int soundResId) {
if (intent == null) {
throw new IllegalArgumentException("'intent' must not be null.");
}
intent.putExtra(CommonParam.EXTRA_SOUND_ON_SENT, soundResId);
}
/**
* KuShiKaTsu {@link Intent}
* KuShiKaTsu
*
*
* <p>
* {@link Activity#startActivityForResult(Intent, int)}
* {@code activity} {@code onActivityResult()}
*
* </p>
* <p>
* {@link Activity#startActivity(Intent)}
*
* </p>
*
* @param activity
* KuShiKaTsu
*
* @param intent
* KuShiKaTsu {@link Intent} {@code null}
* @param requestCode
* KuShiKaTsu
* @return
* KuShiKaTsu {@code true}
* {@code false}
* @throws IllegalArgumentException
* {@code null} {@code null}
*/
public static boolean startKushikatsuForResult(final Activity activity,
final Intent intent, final int requestCode, int installRequestCode) {
if (activity == null) {
throw new IllegalArgumentException("'activity' must not be null.");
}
if (!isKushikatsuInstalled(activity)) {
//
final Intent intentForInstall = buildIntentForKushikatsuInstall();
activity.startActivityForResult(intentForInstall,
installRequestCode);
return false;
}
//
activity.startActivityForResult(intent, requestCode);
return true;
}
}
|