1package com.android.providers.downloads;
2
3import android.app.Notification;
4import android.content.Intent;
5import android.content.pm.PackageManager.NameNotFoundException;
6import android.net.ConnectivityManager;
7import android.net.NetworkInfo;
8import android.test.AssertionFailedError;
9
10import java.util.ArrayList;
11import java.util.HashMap;
12import java.util.LinkedList;
13import java.util.List;
14import java.util.Map;
15import java.util.Queue;
16
17public class FakeSystemFacade implements SystemFacade {
18    long mTimeMillis = 0;
19    Integer mActiveNetworkType = ConnectivityManager.TYPE_WIFI;
20    boolean mIsRoaming = false;
21    Long mMaxBytesOverMobile = null;
22    Long mRecommendedMaxBytesOverMobile = null;
23    List<Intent> mBroadcastsSent = new ArrayList<Intent>();
24    Map<Long,Notification> mActiveNotifications = new HashMap<Long,Notification>();
25    List<Notification> mCanceledNotifications = new ArrayList<Notification>();
26    Queue<Thread> mStartedThreads = new LinkedList<Thread>();
27    private boolean returnActualTime = false;
28
29    void incrementTimeMillis(long delta) {
30        mTimeMillis += delta;
31    }
32
33    public long currentTimeMillis() {
34        if (returnActualTime) {
35            return System.currentTimeMillis();
36        }
37        return mTimeMillis;
38    }
39
40    public NetworkInfo getActiveNetworkInfo(int uid) {
41        if (mActiveNetworkType == null) {
42            return null;
43        } else {
44            return new NetworkInfo(mActiveNetworkType, 0, null, null);
45        }
46    }
47
48    public boolean isNetworkRoaming() {
49        return mIsRoaming;
50    }
51
52    public Long getMaxBytesOverMobile() {
53        return mMaxBytesOverMobile ;
54    }
55
56    public Long getRecommendedMaxBytesOverMobile() {
57        return mRecommendedMaxBytesOverMobile ;
58    }
59
60    @Override
61    public void sendBroadcast(Intent intent) {
62        mBroadcastsSent.add(intent);
63    }
64
65    @Override
66    public boolean userOwnsPackage(int uid, String pckg) throws NameNotFoundException {
67        return true;
68    }
69
70    @Override
71    public void postNotification(long id, Notification notification) {
72        if (notification == null) {
73            throw new AssertionFailedError("Posting null notification");
74        }
75        mActiveNotifications.put(id, notification);
76    }
77
78    @Override
79    public void cancelNotification(long id) {
80        Notification notification = mActiveNotifications.remove(id);
81        if (notification != null) {
82            mCanceledNotifications.add(notification);
83        }
84    }
85
86    @Override
87    public void cancelAllNotifications() {
88        for (long id : mActiveNotifications.keySet()) {
89            cancelNotification(id);
90        }
91    }
92
93    public boolean startThreadsWithoutWaiting = false;
94    public void setStartThreadsWithoutWaiting(boolean flag) {
95        this.startThreadsWithoutWaiting = flag;
96    }
97
98    @Override
99    public void startThread(Thread thread) {
100        if (startThreadsWithoutWaiting) {
101            thread.start();
102        } else {
103            mStartedThreads.add(thread);
104        }
105    }
106
107    public void runAllThreads() {
108        while (!mStartedThreads.isEmpty()) {
109            mStartedThreads.poll().run();
110        }
111    }
112
113    public void setReturnActualTime(boolean flag) {
114        returnActualTime = flag;
115    }
116}
117