1/*
2 * Copyright (C) 2015 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.internal.policy;
18
19import android.content.Context;
20import android.content.res.AssetManager;
21import android.content.res.Resources;
22import android.view.ContextThemeWrapper;
23import android.view.WindowManager;
24import android.view.WindowManagerImpl;
25
26import java.lang.ref.WeakReference;
27
28/**
29 * Context for decor views which can be seeded with pure application context and not depend on the
30 * activity, but still provide some of the facilities that Activity has,
31 * e.g. themes, activity-based resources, etc.
32 *
33 * @hide
34 */
35class DecorContext extends ContextThemeWrapper {
36    private PhoneWindow mPhoneWindow;
37    private WindowManager mWindowManager;
38    private Resources mActivityResources;
39
40    private WeakReference<Context> mActivityContext;
41
42    public DecorContext(Context context, Context activityContext) {
43        super(context, null);
44        mActivityContext = new WeakReference<>(activityContext);
45        mActivityResources = activityContext.getResources();
46    }
47
48    void setPhoneWindow(PhoneWindow phoneWindow) {
49        mPhoneWindow = phoneWindow;
50        mWindowManager = null;
51    }
52
53    @Override
54    public Object getSystemService(String name) {
55        if (Context.WINDOW_SERVICE.equals(name)) {
56            if (mWindowManager == null) {
57                WindowManagerImpl wm =
58                        (WindowManagerImpl) super.getSystemService(Context.WINDOW_SERVICE);
59                mWindowManager = wm.createLocalWindowManager(mPhoneWindow);
60            }
61            return mWindowManager;
62        }
63        return super.getSystemService(name);
64    }
65
66    @Override
67    public Resources getResources() {
68        Context activityContext = mActivityContext.get();
69        // Attempt to update the local cached Resources from the activity context. If the activity
70        // is no longer around, return the old cached values.
71        if (activityContext != null) {
72            mActivityResources = activityContext.getResources();
73        }
74
75        return mActivityResources;
76    }
77
78    @Override
79    public AssetManager getAssets() {
80        return mActivityResources.getAssets();
81    }
82}
83