1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.googlecode.android_scripting.facade.telephony; 18 19 import android.app.Service; 20 import android.content.Context; 21 import android.net.TetheringManager; 22 import android.net.TetheringManager.OnTetheringEntitlementResultListener; 23 import android.telephony.CarrierConfigManager; 24 25 import com.googlecode.android_scripting.Log; 26 import com.googlecode.android_scripting.facade.AndroidFacade; 27 import com.googlecode.android_scripting.facade.FacadeManager; 28 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 29 import com.googlecode.android_scripting.rpc.Rpc; 30 import com.googlecode.android_scripting.rpc.RpcParameter; 31 32 import java.util.concurrent.CompletableFuture; 33 import java.util.concurrent.TimeUnit; 34 35 public class CarrierConfigFacade extends RpcReceiver { 36 private final Service mService; 37 private final AndroidFacade mAndroidFacade; 38 private final CarrierConfigManager mCarrierConfigManager; 39 private final TetheringManager mTetheringManager; 40 CarrierConfigFacade(FacadeManager manager)41 public CarrierConfigFacade(FacadeManager manager) { 42 super(manager); 43 mService = manager.getService(); 44 mAndroidFacade = manager.getReceiver(AndroidFacade.class); 45 mCarrierConfigManager = 46 (CarrierConfigManager)mService.getSystemService(Context.CARRIER_CONFIG_SERVICE); 47 mTetheringManager = (TetheringManager) mService.getSystemService(Context.TETHERING_SERVICE); 48 49 } 50 51 private class EntitlementResultListener implements OnTetheringEntitlementResultListener { 52 private final CompletableFuture<Integer> mFuture = new CompletableFuture<>(); 53 54 @Override onTetheringEntitlementResult(int result)55 public void onTetheringEntitlementResult(int result) { 56 mFuture.complete(result); 57 } 58 get(int timeout, TimeUnit unit)59 public int get(int timeout, TimeUnit unit) throws Exception { 60 return mFuture.get(timeout, unit); 61 } 62 63 } 64 65 @Rpc(description = "Tethering Entitlement Check") carrierConfigIsTetheringModeAllowed( @pcParametername="mode") String mode, @RpcParameter(name="timeout") Integer timeout)66 public boolean carrierConfigIsTetheringModeAllowed( 67 @RpcParameter(name="mode") String mode, 68 @RpcParameter(name="timeout") Integer timeout) { 69 final int tetheringType; 70 if (mode.equals("wifi")){ 71 tetheringType = TetheringManager.TETHERING_WIFI; 72 } else if (mode.equals("usb")) { 73 tetheringType = TetheringManager.TETHERING_USB; 74 } else if (mode.equals("bluetooth")) { 75 tetheringType = TetheringManager.TETHERING_BLUETOOTH; 76 } else { 77 tetheringType = TetheringManager.TETHERING_INVALID; 78 } 79 80 final EntitlementResultListener listener = new EntitlementResultListener(); 81 mTetheringManager.requestLatestTetheringEntitlementResult(tetheringType, true, 82 c -> c.run(), listener); 83 int result; 84 try{ 85 result = listener.get(timeout, TimeUnit.MILLISECONDS); 86 } catch (Exception e) { 87 Log.d("phoneTetherCheck exception" + e.toString()); 88 return false; 89 } 90 91 return result == TetheringManager.TETHER_ERROR_NO_ERROR; 92 } 93 94 @Override shutdown()95 public void shutdown() { 96 97 } 98 } 99