Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.59 KB | None | 0 0
  1.  
  2. package com.secrethq.utils;
  3.  
  4. import java.lang.ref.WeakReference;
  5. import java.io.File;
  6. import java.io.FileFilter;
  7. import java.io.UnsupportedEncodingException;
  8. import java.security.MessageDigest;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.util.regex.Pattern;
  11.  
  12. import org.cocos2dx.lib.Cocos2dxActivity;
  13.  
  14. import com.google.android.gms.R;
  15. import com.google.android.gms.ads.AdRequest;
  16. import com.google.android.gms.common.ConnectionResult;
  17. import com.google.android.gms.common.GooglePlayServicesUtil;
  18. import com.google.android.gms.common.api.GoogleApiClient;
  19. import com.google.android.gms.games.Games;
  20. import com.google.android.gms.games.Player;
  21. import com.google.android.gms.plus.Plus;
  22. import com.secrethq.ads.PTAdAdMobBridge;
  23.  
  24. import android.app.AlertDialog;
  25. import android.app.Dialog;
  26. import android.app.Activity;
  27. import android.content.*;
  28. import android.content.IntentSender.SendIntentException;
  29. import android.net.Uri;
  30. import android.os.Bundle;
  31. import android.util.Log;
  32. import android.view.View;
  33.  
  34. import android.app.UiModeManager;
  35. import android.content.res.Configuration;
  36.  
  37.  
  38. public class PTServicesBridge
  39. implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
  40. private static PTServicesBridge sInstance;
  41. private static final String TAG = "PTServicesBridge";
  42.  
  43. private static native String getLeaderboardId();
  44. private static native void warningMessageClicked(boolean accepted);
  45.  
  46. private static Cocos2dxActivity activity;
  47. private static WeakReference<Cocos2dxActivity> s_activity;
  48.  
  49. private static GoogleApiClient mGoogleApiClient;
  50.  
  51. private static String urlString;
  52. private static int scoreValue;
  53.  
  54. public static final int RC_SIGN_IN = 9001;
  55. private static final int REQUEST_LEADERBOARD = 5000;
  56.  
  57. public static PTServicesBridge instance() {
  58. if (sInstance == null)
  59. sInstance = new PTServicesBridge();
  60. return sInstance;
  61. }
  62.  
  63. public static void initBridge(Cocos2dxActivity activity, String appId){
  64. Log.v(TAG, "PTServicesBridge -- INIT");
  65.  
  66. PTServicesBridge.s_activity = new WeakReference<Cocos2dxActivity>(activity);
  67. PTServicesBridge.activity = activity;
  68.  
  69. if(appId == null || appId.length() == 0 || appId.matches("[0-9]+") == false){
  70. return;
  71. }
  72.  
  73. // Create a GoogleApiClient instance
  74. PTServicesBridge.mGoogleApiClient = new GoogleApiClient.Builder(PTServicesBridge.activity)
  75. .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
  76. .addApi(Games.API).addScope(Games.SCOPE_GAMES)
  77. .addConnectionCallbacks(instance())
  78. .addOnConnectionFailedListener(instance())
  79. .build();
  80. }
  81.  
  82.  
  83. public static void openShareWidget( String message ){
  84. Log.v(TAG, "PTServicesBridge -- openShareWidget with text:" + message);
  85. Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
  86. sharingIntent.setType("text/plain");
  87. sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
  88. PTServicesBridge.activity.startActivity(Intent.createChooser(sharingIntent, "Share" ));
  89. }
  90.  
  91. public static int availableProcessors() {
  92. int processorsNum = Runtime.getRuntime().availableProcessors();
  93. Log.d(TAG, "availableProcessors: " + processorsNum);
  94. return processorsNum;
  95. }
  96.  
  97. public static int getCoresNumber() {
  98.  
  99. class CpuFilter implements FileFilter {
  100.  
  101. @Override
  102. public boolean accept(File pathname) {
  103. //Check if filename is "cpu", followed by a single digit number
  104. if(Pattern.matches("cpu[0-9]+", pathname.getName())) {
  105. return true;
  106. }
  107.  
  108. return false;
  109. }
  110. }
  111.  
  112. try {
  113. //Get directory containing CPU info
  114. File dir = new File("/sys/devices/system/cpu/");
  115.  
  116. //Filter to only list the devices we care about
  117. File[] files = dir.listFiles(new CpuFilter());
  118. Log.d(TAG, "CPU Count: "+files.length);
  119.  
  120. //Return the number of cores (virtual CPU devices)
  121. return files.length;
  122.  
  123. } catch(Exception e) {
  124. //Print exception
  125. Log.d(TAG, "CPU Count: Failed.");
  126. e.printStackTrace();
  127.  
  128. //Default to return 1 core
  129. return 1;
  130. }
  131. }
  132.  
  133. public static void openUrl( String url ){
  134. Log.v(TAG, "PTServicesBridge -- Open URL " + url);
  135.  
  136. PTServicesBridge.urlString = url;
  137.  
  138. PTServicesBridge.s_activity.get().runOnUiThread( new Runnable() {
  139. public void run() {
  140. try {
  141. final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(PTServicesBridge.urlString));
  142. PTServicesBridge.activity.startActivity(intent);
  143. } catch(Exception e) {
  144. //Print exception
  145. Log.d(TAG, "OpenURL: Failed.");
  146. e.printStackTrace();
  147. }
  148. }
  149. });
  150. }
  151.  
  152.  
  153. public static void showLeaderboard( ){
  154. Log.v(TAG, "PTServicesBridge -- Show Leaderboard ");
  155.  
  156. if(PTServicesBridge.mGoogleApiClient == null || PTServicesBridge.mGoogleApiClient.isConnected() == false){
  157. Log.e(TAG, "Google play Servioces is not sigend");
  158. return;
  159. }
  160.  
  161. PTServicesBridge.s_activity.get().runOnUiThread( new Runnable() {
  162. public void run() {
  163. String leaderboardId = PTServicesBridge.getLeaderboardId();
  164. if(leaderboardId == null || leaderboardId.isEmpty()){
  165. return;
  166. }
  167. PTServicesBridge.activity.startActivityForResult(Games.Leaderboards.getLeaderboardIntent(PTServicesBridge.mGoogleApiClient,
  168. leaderboardId), REQUEST_LEADERBOARD);
  169. }
  170. });
  171. }
  172.  
  173. public static void showCustomFullScreenAd() {
  174. Log.e(TAG, "PTServicesBridge -- showCustomFullScreenAd");
  175. }
  176.  
  177. public static void loadingDidComplete() {
  178. Log.e(TAG, "PTServicesBridge -- loadingDidComplete");
  179. }
  180.  
  181. public static void submitScrore( int score ){
  182. Log.v(TAG, "PTServicesBridge -- Submit Score " + score);
  183.  
  184. if(PTServicesBridge.mGoogleApiClient == null || PTServicesBridge.mGoogleApiClient.isConnected() == false){
  185. Log.e(TAG, "Google play Servioces is not sigend");
  186. return;
  187. }
  188.  
  189. String leaderboardId = PTServicesBridge.getLeaderboardId();
  190. if(leaderboardId == null || leaderboardId.isEmpty()){
  191. return;
  192. }
  193. PTServicesBridge.scoreValue = score;
  194.  
  195. if ( PTServicesBridge.mGoogleApiClient.isConnected() ) {
  196. Games.Leaderboards.submitScore(PTServicesBridge.mGoogleApiClient, leaderboardId, PTServicesBridge.scoreValue);
  197. }
  198. }
  199.  
  200. public static boolean isRunningOnTV(){
  201. UiModeManager uiModeManager = (UiModeManager)PTServicesBridge.activity.getSystemService( Context.UI_MODE_SERVICE );
  202. if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
  203. Log.d("DeviceTypeRuntimeCheck", "Running on a TV Device");
  204. return true;
  205.  
  206. } else {
  207. Log.d("DeviceTypeRuntimeCheck", "Running on a non-TV Device");
  208. return false;
  209.  
  210. }
  211. }
  212.  
  213. public static void showFacebookPage( final String facebookURL, final String facebookID){
  214. Log.v(TAG, "Show facebook page for URL: " + facebookURL + " ID: " + facebookID);
  215.  
  216. PTServicesBridge.s_activity.get().runOnUiThread( new Runnable() {
  217. public void run() {
  218. try {
  219. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + facebookID));
  220. PTServicesBridge.activity.startActivity(intent);
  221. } catch(Exception e) {
  222. Log.v(TAG, "Show facebook FAILED going to exception handler : " + e.getMessage());
  223. try {
  224. PTServicesBridge.activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse( facebookURL )));
  225. } catch (Exception e2) {
  226. Log.v(TAG, "Show facebook exception handle FAILED : " + e2.getMessage());
  227. }
  228.  
  229. }
  230. }
  231. });
  232. }
  233.  
  234. public static void showWarningMessage(final String message){
  235. Log.v(TAG, "Show warning with message: " + message);
  236. PTServicesBridge.s_activity.get().runOnUiThread( new Runnable() {
  237. public void run() {
  238. AlertDialog.Builder dlgAlert = new AlertDialog.Builder( PTServicesBridge.activity );
  239.  
  240. dlgAlert.setMessage(message);
  241. dlgAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  242. public void onClick(DialogInterface dialog, int which) {
  243. PTServicesBridge.warningMessageClicked( false );
  244. }
  245. });
  246. dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  247. public void onClick(DialogInterface dialog, int which) {
  248. PTServicesBridge.warningMessageClicked( true );
  249. }
  250. });
  251. dlgAlert.setCancelable(true);
  252. dlgAlert.create().show();
  253. }
  254. });
  255.  
  256. }
  257.  
  258. /*public static void loginGameServices( ){
  259. Log.v(TAG, "PTServicesBridge -- Login Game Services ");
  260.  
  261. if(PTServicesBridge.mGoogleApiClient != null){
  262. PTServicesBridge.mGoogleApiClient.connect();
  263. }
  264. }*/
  265.  
  266.  
  267. public static boolean isGameServiceAvialable( ){
  268. Log.v(TAG, "PTServicesBridge -- Is Game Service Avialable ");
  269.  
  270. return (PTServicesBridge.mGoogleApiClient != null && PTServicesBridge.mGoogleApiClient.isConnected());
  271. }
  272.  
  273. @Override
  274. public void onConnected(Bundle arg0) {
  275. Log.v(TAG, "PTServicesBridge -- API Client Connected bundle:" + arg0);
  276. }
  277.  
  278. @Override
  279. public void onConnectionSuspended(int arg0) {
  280. Log.v(TAG, "PTServicesBridge -- API Client Connection Suspended ");
  281. }
  282.  
  283. @Override
  284. public void onConnectionFailed(ConnectionResult connectionResult) {
  285. Log.v(TAG, "PTServicesBridge -- API Client Connection FAILED:" + connectionResult);
  286.  
  287. if(connectionResult.hasResolution()){
  288. try {
  289. connectionResult.startResolutionForResult(activity, RC_SIGN_IN);
  290. } catch (SendIntentException e) {
  291. mGoogleApiClient.connect();
  292. }
  293. }
  294. }
  295.  
  296. public void onActivityResult(int requestCode, int responseCode, Intent intent){
  297. if(requestCode == RC_SIGN_IN && responseCode == -1){
  298. mGoogleApiClient.connect();
  299. }
  300. }
  301.  
  302. public static String sha1( byte[] data, int length) throws NoSuchAlgorithmException, UnsupportedEncodingException {
  303. MessageDigest md = MessageDigest.getInstance("SHA-1");
  304. md.update(data, 0, length);
  305. byte[] sha1hash = md.digest();
  306. return convertToHex(sha1hash);
  307. }
  308.  
  309.  
  310. private static String convertToHex(byte[] data) {
  311. StringBuilder buf = new StringBuilder();
  312. for (byte b : data) {
  313. int halfbyte = (b >>> 4) & 0x0F;
  314. int two_halfs = 0;
  315. do {
  316. buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));
  317. halfbyte = b & 0x0F;
  318. } while (two_halfs++ < 1);
  319. }
  320. return buf.toString();
  321. }
  322.  
  323.  
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement