RealSystemFacade.java revision 8ac10e0e0667a4fe35191deebb5fa9786bf4226c
1/*
2 * Copyright (C) 2008 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
17package com.android.providers.downloads;
18
19import android.app.DownloadManager;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager.NameNotFoundException;
23import android.net.ConnectivityManager;
24import android.net.NetworkInfo;
25import android.telephony.TelephonyManager;
26import android.util.Log;
27
28class RealSystemFacade implements SystemFacade {
29    private Context mContext;
30
31    public RealSystemFacade(Context context) {
32        mContext = context;
33    }
34
35    @Override
36    public long currentTimeMillis() {
37        return System.currentTimeMillis();
38    }
39
40    @Override
41    public NetworkInfo getActiveNetworkInfo(int uid) {
42        ConnectivityManager connectivity =
43                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
44        if (connectivity == null) {
45            Log.w(Constants.TAG, "couldn't get connectivity manager");
46            return null;
47        }
48
49        final NetworkInfo activeInfo = connectivity.getActiveNetworkInfoForUid(uid);
50        if (activeInfo == null && Constants.LOGVV) {
51            Log.v(Constants.TAG, "network is not available");
52        }
53        return activeInfo;
54    }
55
56    @Override
57    public boolean isActiveNetworkMetered() {
58        final ConnectivityManager conn = ConnectivityManager.from(mContext);
59        return conn.isActiveNetworkMetered();
60    }
61
62    @Override
63    public boolean isNetworkRoaming() {
64        ConnectivityManager connectivity =
65            (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
66        if (connectivity == null) {
67            Log.w(Constants.TAG, "couldn't get connectivity manager");
68            return false;
69        }
70
71        NetworkInfo info = connectivity.getActiveNetworkInfo();
72        boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
73        boolean isRoaming = isMobile && TelephonyManager.getDefault().isNetworkRoaming();
74        if (Constants.LOGVV && isRoaming) {
75            Log.v(Constants.TAG, "network is roaming");
76        }
77        return isRoaming;
78    }
79
80    @Override
81    public Long getMaxBytesOverMobile() {
82        return DownloadManager.getMaxBytesOverMobile(mContext);
83    }
84
85    @Override
86    public Long getRecommendedMaxBytesOverMobile() {
87        return DownloadManager.getRecommendedMaxBytesOverMobile(mContext);
88    }
89
90    @Override
91    public void sendBroadcast(Intent intent) {
92        mContext.sendBroadcast(intent);
93    }
94
95    @Override
96    public boolean userOwnsPackage(int uid, String packageName) throws NameNotFoundException {
97        return mContext.getPackageManager().getApplicationInfo(packageName, 0).uid == uid;
98    }
99}
100