FileDocCategorySizeDatePackage
RateController.javaAPI DocAndroid 1.5 API5293Wed May 06 22:42:46 BST 2009com.android.mms.util

RateController

public class RateController extends Object

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final boolean
LOCAL_LOGV
private static final int
RATE_LIMIT
private static final long
ONE_HOUR
private static final int
NO_ANSWER
private static final int
ANSWER_YES
private static final int
ANSWER_NO
public static final int
ANSWER_TIMEOUT
public static final String
RATE_LIMIT_SURPASSED_ACTION
public static final String
RATE_LIMIT_CONFIRMED_ACTION
private static RateController
sInstance
private static boolean
sMutexLock
private final android.content.Context
mContext
private int
mAnswer
private final android.content.BroadcastReceiver
mBroadcastReceiver
Constructors Summary
private RateController(android.content.Context context)


       
        mContext = context;
    
Methods Summary
public static com.android.mms.util.RateControllergetInstance()

        if (sInstance == null) {
            throw new IllegalStateException("Uninitialized.");
        }
        return sInstance;
    
public static voidinit(android.content.Context context)

        if (LOCAL_LOGV) {
            Log.v(TAG, "RateController.init()");
        }

        if (sInstance != null) {
            Log.w(TAG, "Already initialized.");
        }
        sInstance = new RateController(context);
    
public synchronized booleanisAllowedByUser()

        while (sMutexLock) {
            try {
                wait();
            } catch (InterruptedException _) {
                 // Ignore it.
            }
        }
        sMutexLock = true;

        mContext.registerReceiver(mBroadcastReceiver,
                new IntentFilter(RATE_LIMIT_CONFIRMED_ACTION));

        mAnswer = NO_ANSWER;
        try {
            Intent intent = new Intent(RATE_LIMIT_SURPASSED_ACTION);
            // Using NEW_TASK here is necessary because we're calling
            // startActivity from outside an activity.
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
            return waitForAnswer() == ANSWER_YES;
        } finally {
            mContext.unregisterReceiver(mBroadcastReceiver);
            sMutexLock = false;
            notifyAll();
        }
    
public final booleanisLimitSurpassed()

        long oneHourAgo = System.currentTimeMillis() - ONE_HOUR;
        Cursor c = SqliteWrapper.query(mContext, mContext.getContentResolver(),
                Rate.CONTENT_URI, new String[] { "COUNT(*) AS rate" },
                Rate.SENT_TIME + ">" + oneHourAgo, null, null);
        if (c != null) {
            try {
                if (c.moveToFirst()) {
                    return c.getInt(0) >= RATE_LIMIT;
                }
            } finally {
                c.close();
            }
        }
        return false;
    
public final voidupdate()

        ContentValues values = new ContentValues(1);
        values.put(Rate.SENT_TIME, System.currentTimeMillis());
        SqliteWrapper.insert(mContext, mContext.getContentResolver(),
                             Rate.CONTENT_URI, values);
    
private synchronized intwaitForAnswer()

        for (int t = 0; (mAnswer == NO_ANSWER) && (t < ANSWER_TIMEOUT); t += 1000) {
            try {
                if (LOCAL_LOGV) {
                    Log.v(TAG, "Waiting for answer..." + t / 1000);
                }
                wait(1000L);
            } catch (InterruptedException _) {
                 // Ignore it.
            }
        }
        return mAnswer;