1 package com.android.providers.downloads; 2 3 import static org.mockito.Matchers.any; 4 import static org.mockito.Mockito.mock; 5 import static org.mockito.Mockito.when; 6 7 import android.content.Context; 8 import android.content.Intent; 9 import android.content.pm.PackageManager.NameNotFoundException; 10 import android.net.ConnectivityManager; 11 import android.net.Network; 12 import android.net.NetworkInfo; 13 import android.net.NetworkInfo.DetailedState; 14 15 import org.mockito.invocation.InvocationOnMock; 16 import org.mockito.stubbing.Answer; 17 18 import java.io.IOException; 19 import java.net.URL; 20 import java.net.URLConnection; 21 import java.security.GeneralSecurityException; 22 import java.util.ArrayList; 23 import java.util.List; 24 import javax.net.ssl.SSLContext; 25 26 public class FakeSystemFacade implements SystemFacade { 27 long mTimeMillis = 0; 28 Integer mActiveNetworkType = ConnectivityManager.TYPE_WIFI; 29 boolean mIsRoaming = false; 30 boolean mIsMetered = false; 31 long mMaxBytesOverMobile = Long.MAX_VALUE; 32 long mRecommendedMaxBytesOverMobile = Long.MAX_VALUE; 33 List<Intent> mBroadcastsSent = new ArrayList<Intent>(); 34 boolean mCleartextTrafficPermitted = true; 35 private boolean mReturnActualTime = false; 36 private SSLContext mSSLContext = null; 37 setUp()38 public void setUp() { 39 mTimeMillis = 0; 40 mActiveNetworkType = ConnectivityManager.TYPE_WIFI; 41 mIsRoaming = false; 42 mIsMetered = false; 43 mMaxBytesOverMobile = Long.MAX_VALUE; 44 mRecommendedMaxBytesOverMobile = Long.MAX_VALUE; 45 mBroadcastsSent.clear(); 46 mReturnActualTime = false; 47 try { 48 mSSLContext = SSLContext.getDefault(); 49 } catch (GeneralSecurityException e) { 50 throw new RuntimeException(e); 51 } 52 } 53 incrementTimeMillis(long delta)54 void incrementTimeMillis(long delta) { 55 mTimeMillis += delta; 56 } 57 58 @Override currentTimeMillis()59 public long currentTimeMillis() { 60 if (mReturnActualTime) { 61 return System.currentTimeMillis(); 62 } 63 return mTimeMillis; 64 } 65 66 @Override getActiveNetwork(int uid, boolean ignoreBlocked)67 public Network getActiveNetwork(int uid, boolean ignoreBlocked) { 68 if (mActiveNetworkType == null) { 69 return null; 70 } else { 71 final Network network = mock(Network.class); 72 try { 73 when(network.openConnection(any())).then(new Answer<URLConnection>() { 74 @Override 75 public URLConnection answer(InvocationOnMock invocation) throws Throwable { 76 final URL url = (URL) invocation.getArguments()[0]; 77 return url.openConnection(); 78 } 79 }); 80 } catch (IOException ignored) { 81 } 82 return network; 83 } 84 } 85 86 @Override getNetworkInfo(Network network, int uid, boolean ignoreBlocked)87 public NetworkInfo getNetworkInfo(Network network, int uid, boolean ignoreBlocked) { 88 if (mActiveNetworkType == null) { 89 return null; 90 } else { 91 final NetworkInfo info = new NetworkInfo(mActiveNetworkType, 0, null, null); 92 info.setDetailedState(DetailedState.CONNECTED, null, null); 93 info.setRoaming(mIsRoaming); 94 info.setMetered(mIsMetered); 95 return info; 96 } 97 } 98 99 @Override getMaxBytesOverMobile()100 public long getMaxBytesOverMobile() { 101 return mMaxBytesOverMobile; 102 } 103 104 @Override getRecommendedMaxBytesOverMobile()105 public long getRecommendedMaxBytesOverMobile() { 106 return mRecommendedMaxBytesOverMobile; 107 } 108 109 @Override sendBroadcast(Intent intent)110 public void sendBroadcast(Intent intent) { 111 mBroadcastsSent.add(intent); 112 } 113 114 @Override userOwnsPackage(int uid, String pckg)115 public boolean userOwnsPackage(int uid, String pckg) throws NameNotFoundException { 116 return true; 117 } 118 119 @Override isCleartextTrafficPermitted(int uid)120 public boolean isCleartextTrafficPermitted(int uid) { 121 return mCleartextTrafficPermitted; 122 } 123 124 @Override getSSLContextForPackage(Context context, String pckg)125 public SSLContext getSSLContextForPackage(Context context, String pckg) { 126 return mSSLContext; 127 } 128 setSSLContext(SSLContext context)129 public void setSSLContext(SSLContext context) { 130 mSSLContext = context; 131 } 132 setReturnActualTime(boolean flag)133 public void setReturnActualTime(boolean flag) { 134 mReturnActualTime = flag; 135 } 136 } 137