RealSystemFacade.java revision 9b4371da26f27cfa85d78e0bd4728309f9a445c9
1package com.android.providers.downloads; 2 3import android.app.DownloadManager; 4import android.app.Notification; 5import android.app.NotificationManager; 6import android.content.Context; 7import android.content.Intent; 8import android.content.pm.PackageManager.NameNotFoundException; 9import android.net.ConnectivityManager; 10import android.net.NetworkInfo; 11import android.provider.Settings; 12import android.provider.Settings.SettingNotFoundException; 13import android.telephony.TelephonyManager; 14import android.util.Log; 15 16class RealSystemFacade implements SystemFacade { 17 private Context mContext; 18 private NotificationManager mNotificationManager; 19 20 public RealSystemFacade(Context context) { 21 mContext = context; 22 mNotificationManager = (NotificationManager) 23 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 24 } 25 26 public long currentTimeMillis() { 27 return System.currentTimeMillis(); 28 } 29 30 public Integer getActiveNetworkType() { 31 ConnectivityManager connectivity = 32 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 33 if (connectivity == null) { 34 Log.w(Constants.TAG, "couldn't get connectivity manager"); 35 return null; 36 } 37 38 NetworkInfo activeInfo = connectivity.getActiveNetworkInfo(); 39 if (activeInfo == null) { 40 if (Constants.LOGVV) { 41 Log.v(Constants.TAG, "network is not available"); 42 } 43 return null; 44 } 45 return activeInfo.getType(); 46 } 47 48 public boolean isNetworkRoaming() { 49 ConnectivityManager connectivity = 50 (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 51 if (connectivity == null) { 52 Log.w(Constants.TAG, "couldn't get connectivity manager"); 53 return false; 54 } 55 56 NetworkInfo info = connectivity.getActiveNetworkInfo(); 57 boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE); 58 boolean isRoaming = isMobile && TelephonyManager.getDefault().isNetworkRoaming(); 59 if (Constants.LOGVV && isRoaming) { 60 Log.v(Constants.TAG, "network is roaming"); 61 } 62 return isRoaming; 63 } 64 65 public Long getMaxBytesOverMobile() { 66 return DownloadManager.getMaxBytesOverMobile(mContext); 67 } 68 69 @Override 70 public Long getRecommendedMaxBytesOverMobile() { 71 return DownloadManager.getRecommendedMaxBytesOverMobile(mContext); 72 } 73 74 @Override 75 public void sendBroadcast(Intent intent) { 76 mContext.sendBroadcast(intent); 77 } 78 79 @Override 80 public boolean userOwnsPackage(int uid, String packageName) throws NameNotFoundException { 81 return mContext.getPackageManager().getApplicationInfo(packageName, 0).uid == uid; 82 } 83 84 @Override 85 public void postNotification(long id, Notification notification) { 86 /** 87 * TODO: The system notification manager takes ints, not longs, as IDs, but the download 88 * manager uses IDs take straight from the database, which are longs. This will have to be 89 * dealt with at some point. 90 */ 91 mNotificationManager.notify((int) id, notification); 92 } 93 94 @Override 95 public void cancelNotification(long id) { 96 mNotificationManager.cancel((int) id); 97 } 98 99 @Override 100 public void cancelAllNotifications() { 101 mNotificationManager.cancelAll(); 102 } 103 104 @Override 105 public void startThread(Thread thread) { 106 thread.start(); 107 } 108} 109