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