Back to project page sdk-android-2.
The source code is released under:
Apache License
If you think the Android project sdk-android-2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2013 Medium Entertainment, Inc. */*w w w . j av a 2 s . co m*/ * 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 com.playhaven.android.diagnostic; import java.util.LinkedHashMap; import java.util.concurrent.Executors; import java.util.concurrent.Future; import android.app.Activity; import android.content.Context; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.jayway.jsonpath.JsonPath; import com.playhaven.android.PlayHaven; import com.playhaven.android.PlayHavenException; import com.playhaven.android.data.Purchase; import com.playhaven.android.req.*; import com.playhaven.android.util.GooglePlayServicesUtil; public class APIValidationIndicators extends Activity { private String mContentUnitId; private String mDispatchModel, mModel, mCUModel; private String mPlacementTag; // This keeps track of both the view ids and their default text. private LinkedHashMap<Integer, String> indicatorIds; @Override public void onCreate(Bundle saved) { super.onCreate(saved); setContentView(R.layout.api_status); indicatorIds = new LinkedHashMap<Integer, String>(); indicatorIds.put(R.id.open_req, "Open Request"); indicatorIds.put(R.id.content_req, "Content Request"); indicatorIds.put(R.id.content_dispatch_req, "Content Dispatch Request"); indicatorIds.put(R.id.content_unit_req, "Content Unit Request"); indicatorIds.put(R.id.gcm_reg_req, "GCM Registration Request"); indicatorIds.put(R.id.gcm_dereg_req, "GCM Deregistration Request"); indicatorIds.put(R.id.purchase_req, "Purchase Request"); indicatorIds.put(R.id.metadata_req, "Metadata Request"); indicatorIds.put(R.id.sub_content_req, "Subcontent Request"); indicatorIds.put(R.id.sub_content_unit_req, "Subcontent Unit Request"); indicatorIds.put(R.id.url_req, "Url Request"); if (!GooglePlayServicesUtil.isGooglePlayServicesAvailable()) { findViewById(R.id.content_dispatch_req).setVisibility(View.GONE); findViewById(R.id.sub_content_unit_req).setVisibility(View.GONE); } for(Integer key : indicatorIds.keySet()) { ((Button) findViewById(key)).setText(indicatorIds.get(key)); } } public void startRequests(View goBtn) { String placementTag = ((EditText) findViewById(R.id.placementBox)).getText().toString(); if(placementTag != null && placementTag.length() > 0) { mPlacementTag = placementTag; } else { ((EditText) findViewById(R.id.placementBox)).setError(getString(R.string.go_error)); return; } // These requests aren't dependent on other results. final int[] freeRequests = { R.id.open_req, R.id.content_req, R.id.gcm_reg_req, R.id.gcm_dereg_req, R.id.purchase_req, R.id.metadata_req }; Thread t = new Thread(){ @Override public void run(){ for(int id : freeRequests) { doRequest(findViewById(id)); } } }; t.start(); } public void doRequest(View v) { updateUI(v.getId(), "Doing request: %s %s", "", R.drawable.test_new); switch(v.getId()){ case R.id.open_req: OpenRequest open = new OpenRequest(); open.setResponseHandler(new MListener(v.getId())); open.send(getApplicationContext()); break; case R.id.gcm_reg_req: PushTrackingRequest deRegReq = new PushTrackingRequest("test", null, null, null); deRegReq.setResponseHandler(new MListener(v.getId())); deRegReq.send(getApplicationContext()); break; case R.id.gcm_dereg_req: PushTrackingRequest regReq = new PushTrackingRequest("", null, null, null); regReq.setResponseHandler(new MListener(v.getId())); regReq.send(getApplicationContext()); break; case R.id.purchase_req: Purchase purchase = new Purchase(); purchase.setSKU("test"); purchase.setQuantity(1); purchase.setResult(Purchase.Result.Bought); PurchaseTrackingRequest purchaseRequest = new PurchaseTrackingRequest(purchase); purchaseRequest.setResponseHandler(new MListener(v.getId())); purchaseRequest.send(getApplicationContext()); break; case R.id.content_req: // The url request expects this to be a More Games request. ContentRequest placementRequest = new ContentRequest(mPlacementTag); placementRequest.setResponseHandler(new MListener(v.getId())); placementRequest.send(getApplicationContext()); break; case R.id.content_dispatch_req: try{ ContentDispatchRequest dispatchRequest = new ContentDispatchRequest(mDispatchModel); dispatchRequest.setResponseHandler(new MListener(v.getId())); dispatchRequest.send(getApplicationContext()); } catch (PlayHavenException e) { Log.e(Launcher.TAG, "Error processing content_dispatch_req", e); } break; case R.id.sub_content_req: String scDispatch = null; try { scDispatch = getJson(mModel, "$.response.context.content.skip_dispatch.parameters"); SubcontentRequest subReq = new SubcontentRequest(scDispatch); subReq.setResponseHandler(new MListener(v.getId())); subReq.send(getApplicationContext()); } catch (PlayHavenException e) { Log.e(Launcher.TAG, "Error processing sub_content_req", e); } break; case R.id.metadata_req: MetadataRequest metaReq = new MetadataRequest(mPlacementTag); metaReq.setResponseHandler(new MListener(v.getId())); metaReq.send(getApplicationContext()); break; case R.id.url_req: String firstUrl = getJson(mModel, "$.response.context.content.items[0].buy_dispatch.parameters.url"); UrlRequest urlRequest = new UrlRequest(firstUrl); final Long timestamp = System.currentTimeMillis(); final Future<String> uriFuture = Executors.newSingleThreadExecutor().submit(urlRequest); String url = null; try { url = uriFuture.get(); PlayHaven.v("UrlRequest retrieved: %s", url); } catch (Exception e) { PlayHaven.e("Could not retrieve launch URL from server."); } if(url != null && url.startsWith("market:")){ String tsString = String.format("(%sms)", System.currentTimeMillis() - timestamp); updateUI(R.id.url_req, "Success: %s %s", tsString, R.drawable.test_succeeded); } else { updateUI(R.id.url_req, "Failure: %s, got: %s", url, R.drawable.test_failed); } break; case R.id.content_unit_req: ContentUnitRequest contentReq; if (GooglePlayServicesUtil.isGooglePlayServicesAvailable()) { contentReq = new ContentUnitRequest(null); contentReq.setContentUnitId(mContentUnitId); } else { contentReq = new ContentUnitRequest(mPlacementTag); } contentReq.setMessageId("999999"); contentReq.setResponseHandler(new MListener(v.getId())); contentReq.send(getApplicationContext()); break; case R.id.sub_content_unit_req: String scuDispatch = null; try { scuDispatch = getJson(mCUModel, "$.response.context.content.skip_dispatch.parameters"); SubcontentRequest subReq = new SubcontentRequest(scuDispatch); subReq.setResponseHandler(new MListener(v.getId())); subReq.send(getApplicationContext()); } catch (PlayHavenException e) { Log.e(Launcher.TAG, "Error processing sub_content_unit_req", e); } break; default: break; } } private class MListener implements RequestListener { private int mId; private long mTimestamp; public MListener(int id){ super(); mId = id; mTimestamp = System.currentTimeMillis(); } @Override public void handleResponse(Context context, String json) { boolean responseIsGood = true; String timeMsg = String.format("(%sms)", (System.currentTimeMillis() - mTimestamp)); if(json != null) { // @TODO validate each of the responses more thoroughly, as with some kind of schema. <.< switch(mId) { case R.id.open_req: responseIsGood = (getJson(json, "$.response.precache") != null); break; case R.id.content_req: final Button contentDispatchButton = (Button)findViewById(R.id.content_dispatch_req); if (GooglePlayServicesUtil.isGooglePlayServicesAvailable()) { if(getJson(json, "$.response.content_dispatch.data.params.content_id") == null) { responseIsGood = false; break; } mDispatchModel = json; doRequest(contentDispatchButton); } else { mModel = json; final int[] dependantRequests = { R.id.content_unit_req, R.id.url_req, R.id.sub_content_req, R.id.sub_content_unit_req }; for(Integer id : dependantRequests) { doRequest(findViewById(id)); } } runOnUiThread(new Runnable() { public void run() { contentDispatchButton.setText(indicatorIds.get(R.id.content_dispatch_req)); } }); break; case R.id.content_dispatch_req: try { Uri url = Uri.parse(getJson(json, "$.response.close_ping")); mContentUnitId = url.getQueryParameter("content_id"); } catch (Exception e) { responseIsGood = false; break; } mContentUnitId = mContentUnitId.trim(); // These requests are dependent on the results of the first content request. final int[] dependantRequests = { R.id.content_unit_req, R.id.url_req, R.id.sub_content_req, R.id.sub_content_unit_req }; runOnUiThread(new Runnable() { public void run() { for(Integer id : dependantRequests) { ((Button) findViewById(id)).setText(indicatorIds.get(id)); } } }); mModel = json; for(Integer id : dependantRequests) { doRequest(findViewById(id)); } break; case R.id.content_unit_req: if(getJson(json, "$.response.url") == null) { responseIsGood = false; break; } final Button subcuDispatchButton = (Button)findViewById(R.id.sub_content_unit_req); runOnUiThread(new Runnable() { public void run() { subcuDispatchButton.setText(indicatorIds.get(R.id.sub_content_unit_req)); } }); mCUModel = json; doRequest(subcuDispatchButton); break; case R.id.gcm_reg_req: responseIsGood = (getJson(json, "$.response.push_token") != null); break; case R.id.gcm_dereg_req: responseIsGood = (getJson(json, "$.response.push_token") == null); break; case R.id.purchase_req: responseIsGood = (getJson(json, "$.response") != null); break; case R.id.metadata_req: responseIsGood = ("badge".equals(getJson(json, "$.response.notification.type"))); break; case R.id.sub_content_req: responseIsGood = (getJson(json, "$.response.url") != null); break; case R.id.sub_content_unit_req: responseIsGood = (getJson(json, "$.response.content_dispatch.data.url") != null); break; default: break; } } if(responseIsGood){ updateUI(mId, "Success: %s %s", timeMsg, R.drawable.test_succeeded); } else { updateUI(mId, "Failure: %s%s", "'s response did not validate.", R.drawable.test_failed); } } @Override public void handleResponse(Context context, PlayHavenException e) { updateUI(mId, "Failure: %s %s", e.getMessage(), R.drawable.test_failed); } } private void updateUI(final int id, final String format, final String secondArg, final int drawable){ runOnUiThread(new Runnable() { public void run() { Button button = (Button) findViewById(id); button.setText(String.format(format, indicatorIds.get(id), secondArg)); Drawable icon = getBaseContext().getResources().getDrawable(drawable); button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null ); } }); } private String getJson(String json, String path) { try { return JsonPath.read(json, path).toString(); } catch (Exception e) { return null; } } }