com.google.android.vending.licensing.NaaPolicy.java Source code

Java tutorial

Introduction

Here is the source code for com.google.android.vending.licensing.NaaPolicy.java

Source

package com.google.android.vending.licensing;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;

public class NaaPolicy implements Policy {

    private static final String TAG = "NaaPolicy";
    private static final String DEFAULT_MAX_RETRIES = "2";
    private static final String DEFAULT_RETRY_COUNT = "0";
    private long mMaxRetries;
    private long mRetryCount;
    private int mLastResponse;

    public NaaPolicy() {

        mMaxRetries = Long.parseLong(DEFAULT_MAX_RETRIES);
        mRetryCount = Long.parseLong(DEFAULT_RETRY_COUNT);
    }

    public void processServerResponse(int response, ResponseData rawData) {
        if (response != Policy.RETRY) {
            setRetryCount(0);
        } else {
            setRetryCount(mRetryCount + 1);
        }

        if (response == Policy.LICENSED) {
            Map<String, String> extras = decodeExtras(rawData.extra);
            mLastResponse = response;
            setMaxRetries(extras.get("GR"));
        } else if (response == Policy.NOT_LICENSED) {
            setMaxRetries(DEFAULT_MAX_RETRIES);
        }

        setLastResponse(response);
    }

    private void setLastResponse(int l) {
        mLastResponse = l;
    }

    private void setRetryCount(long c) {
        mRetryCount = c;
    }

    private void setMaxRetries(String maxRetries) {
        Long lMaxRetries;
        try {
            lMaxRetries = Long.parseLong(maxRetries);
        } catch (NumberFormatException e) {
            Log.w(TAG, "Licence retry count (GR) missing, grace period disabled");
            lMaxRetries = 0l;
        }

        mMaxRetries = lMaxRetries;
    }

    public boolean Anumati() {
        if (mLastResponse == Policy.LICENSED) {
            return true;
        } else if (mLastResponse == Policy.RETRY) {

            return (mRetryCount <= mMaxRetries);
        }

        return false;
    }

    private Map<String, String> decodeExtras(String extras) {
        Map<String, String> results = new HashMap<String, String>();
        try {
            URI rawExtras = new URI("?" + extras);
            List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
            for (NameValuePair item : extraList) {
                results.put(item.getName(), item.getValue());
            }
        } catch (URISyntaxException e) {
            Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
        }
        return results;
    }

}