Home | History | Annotate | Download | only in downloads
      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 
     17 package com.android.providers.downloads;
     18 
     19 import android.app.DownloadManager;
     20 import android.app.job.JobParameters;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.ApplicationInfo;
     24 import android.content.pm.PackageInfo;
     25 import android.content.pm.PackageManager;
     26 import android.content.pm.PackageManager.NameNotFoundException;
     27 import android.net.ConnectivityManager;
     28 import android.net.Network;
     29 import android.net.NetworkCapabilities;
     30 import android.net.NetworkInfo;
     31 import android.security.NetworkSecurityPolicy;
     32 import android.security.net.config.ApplicationConfig;
     33 
     34 import com.android.internal.util.ArrayUtils;
     35 
     36 import java.security.GeneralSecurityException;
     37 
     38 import javax.net.ssl.SSLContext;
     39 import javax.net.ssl.TrustManager;
     40 
     41 class RealSystemFacade implements SystemFacade {
     42     private Context mContext;
     43 
     44     public RealSystemFacade(Context context) {
     45         mContext = context;
     46     }
     47 
     48     @Override
     49     public long currentTimeMillis() {
     50         return System.currentTimeMillis();
     51     }
     52 
     53     @Override
     54     public Network getNetwork(JobParameters params) {
     55         return params.getNetwork();
     56     }
     57 
     58     @Override
     59     public NetworkInfo getNetworkInfo(Network network, int uid, boolean ignoreBlocked) {
     60         return mContext.getSystemService(ConnectivityManager.class)
     61                 .getNetworkInfoForUid(network, uid, ignoreBlocked);
     62     }
     63 
     64     @Override
     65     public NetworkCapabilities getNetworkCapabilities(Network network) {
     66         return mContext.getSystemService(ConnectivityManager.class)
     67                 .getNetworkCapabilities(network);
     68     }
     69 
     70     @Override
     71     public long getMaxBytesOverMobile() {
     72         final Long value = DownloadManager.getMaxBytesOverMobile(mContext);
     73         return (value == null) ? Long.MAX_VALUE : value;
     74     }
     75 
     76     @Override
     77     public long getRecommendedMaxBytesOverMobile() {
     78         final Long value = DownloadManager.getRecommendedMaxBytesOverMobile(mContext);
     79         return (value == null) ? Long.MAX_VALUE : value;
     80     }
     81 
     82     @Override
     83     public void sendBroadcast(Intent intent) {
     84         mContext.sendBroadcast(intent);
     85     }
     86 
     87     @Override
     88     public boolean userOwnsPackage(int uid, String packageName) throws NameNotFoundException {
     89         return mContext.getPackageManager().getApplicationInfo(packageName, 0).uid == uid;
     90     }
     91 
     92     @Override
     93     public SSLContext getSSLContextForPackage(Context context, String packageName)
     94             throws GeneralSecurityException {
     95         ApplicationConfig appConfig;
     96         try {
     97             appConfig = NetworkSecurityPolicy.getApplicationConfigForPackage(context, packageName);
     98         } catch (NameNotFoundException e) {
     99             // Unknown package -- fallback to the default SSLContext
    100             return SSLContext.getDefault();
    101         }
    102         SSLContext ctx = SSLContext.getInstance("TLS");
    103         ctx.init(null, new TrustManager[] {appConfig.getTrustManager()}, null);
    104         return ctx;
    105     }
    106 
    107     /**
    108      * Returns whether cleartext network traffic (HTTP) is permitted for the provided package to
    109      * {@code host}.
    110      */
    111     public boolean isCleartextTrafficPermitted(String packageName, String host) {
    112         ApplicationConfig appConfig;
    113         try {
    114             appConfig = NetworkSecurityPolicy.getApplicationConfigForPackage(mContext, packageName);
    115         } catch (NameNotFoundException e) {
    116             // Unknown package -- fail for safety
    117             return false;
    118         }
    119         return appConfig.isCleartextTrafficPermitted(host);
    120     }
    121 }
    122