Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 19e27c05 authored by Xavier Ducrohet's avatar Xavier Ducrohet Committed by Android (Google) Code Review
Browse files

Merge "Fix sdk layout rendering in JB."

parents c6e8811c a421f6c0
Loading
Loading
Loading
Loading
+72 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.policy;

import com.android.ide.common.rendering.api.LayoutLog;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.impl.RenderAction;

import android.content.Context;
import android.view.BridgeInflater;
import android.view.FallbackEventHandler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManagerPolicy;

/**
 * Custom implementation of PolicyManager that does nothing to run in LayoutLib.
 *
 */
public class PolicyManager {

    public static Window makeNewWindow(Context context) {
        // this will likely crash somewhere beyond so we log it.
        Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
                "Call to PolicyManager.makeNewWindow is not supported", null);
        return null;
    }

    public static LayoutInflater makeNewLayoutInflater(Context context) {
        return new BridgeInflater(context, RenderAction.getCurrentContext().getProjectCallback());
    }

    public static WindowManagerPolicy makeNewWindowManager() {
        // this will likely crash somewhere beyond so we log it.
        Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
                "Call to PolicyManager.makeNewWindowManager is not supported", null);
        return null;
    }

    public static FallbackEventHandler makeNewFallbackEventHandler(Context context) {
        return new FallbackEventHandler() {
            @Override
            public void setView(View v) {
            }

            @Override
            public void preDispatchKeyEvent(KeyEvent event) {
            }

            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                return false;
            }
        };
    }
}
+27 −10
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.ide.common.rendering.api.SessionParams;
import com.android.layoutlib.bridge.impl.FontLoader;
import com.android.layoutlib.bridge.impl.RenderDrawable;
import com.android.layoutlib.bridge.impl.RenderSessionImpl;
import com.android.layoutlib.bridge.util.DynamicIdMap;
import com.android.ninepatch.NinePatchChunk;
import com.android.resources.ResourceType;
import com.android.tools.layoutlib.create.MethodAdapter;
@@ -78,7 +79,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
    private final static ReentrantLock sLock = new ReentrantLock();

    /**
     * Maps from id to resource type/name. This is for android.R only.
     * Maps from id to resource type/name. This is for com.android.internal.R
     */
    private final static Map<Integer, Pair<ResourceType, String>> sRMap =
        new HashMap<Integer, Pair<ResourceType, String>>();
@@ -89,11 +90,17 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
    private final static Map<IntArray, String> sRArrayMap = new HashMap<IntArray, String>();
    /**
     * Reverse map compared to sRMap, resource type -> (resource name -> id).
     * This is for android.R only.
     * This is for com.android.internal.R.
     */
    private final static Map<ResourceType, Map<String, Integer>> sRFullMap =
    private final static Map<ResourceType, Map<String, Integer>> sRevRMap =
        new EnumMap<ResourceType, Map<String,Integer>>(ResourceType.class);

    // framework resources are defined as 0x01XX#### where XX is the resource type (layout,
    // drawable, etc...). Using FF as the type allows for 255 resource types before we get a
    // collision which should be fine.
    private final static int DYNAMIC_ID_SEED_START = 0x01ff0000;
    private final static DynamicIdMap sDynamicIds = new DynamicIdMap(DYNAMIC_ID_SEED_START);

    private final static Map<Object, Map<String, SoftReference<Bitmap>>> sProjectBitmapCache =
        new HashMap<Object, Map<String, SoftReference<Bitmap>>>();
    private final static Map<Object, Map<String, SoftReference<NinePatchChunk>>> sProject9PatchCache =
@@ -257,7 +264,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
                ResourceType resType = ResourceType.getEnum(resTypeName);
                if (resType != null) {
                    Map<String, Integer> fullMap = new HashMap<String, Integer>();
                    sRFullMap.put(resType, fullMap);
                    sRevRMap.put(resType, fullMap);

                    for (Field f : inner.getDeclaredFields()) {
                        // only process static final fields. Since the final attribute may have
@@ -459,7 +466,14 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
     *     does not match any resource.
     */
    public static Pair<ResourceType, String> resolveResourceId(int value) {
        return sRMap.get(value);
        Pair<ResourceType, String> pair = sRMap.get(value);
        if (pair == null) {
            pair = sDynamicIds.resolveId(value);
            if (pair == null) {
                System.out.println(String.format("Missing id: %1$08X (%1$d)", value));
            }
        }
        return pair;
    }

    /**
@@ -478,12 +492,17 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
     * @return an {@link Integer} containing the resource id, or null if no resource were found.
     */
    public static Integer getResourceId(ResourceType type, String name) {
        Map<String, Integer> map = sRFullMap.get(type);
        Map<String, Integer> map = sRevRMap.get(type);
        Integer value = null;
        if (map != null) {
            return map.get(name);
            value = map.get(name);
        }

        return null;
        if (value == null) {
            value = sDynamicIds.getId(type, name);
        }

        return value;
    }

    /**
@@ -598,6 +617,4 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
            sFramework9PatchCache.put(value, new SoftReference<NinePatchChunk>(ninePatch));
        }
    }


}
+5 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
@@ -431,6 +432,10 @@ public final class BridgeContext extends Context {
            return null;
        }

        if (POWER_SERVICE.equals(service)) {
            return new PowerManager(new BridgePowerManager(), new Handler());
        }

        throw new UnsupportedOperationException("Unsupported Service: " + service);
    }

+132 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.layoutlib.bridge.android;

import android.os.IBinder;
import android.os.IPowerManager;
import android.os.RemoteException;
import android.os.WorkSource;

/**
 * Fake implementation of IPowerManager.
 *
 */
public class BridgePowerManager implements IPowerManager {

    @Override
    public boolean isScreenOn() throws RemoteException {
        return true;
    }

    @Override
    public IBinder asBinder() {
        // pass for now.
        return null;
    }

    @Override
    public void acquireWakeLock(int arg0, IBinder arg1, String arg2, WorkSource arg3)
            throws RemoteException {
        // pass for now.
    }

    @Override
    public void clearUserActivityTimeout(long arg0, long arg1) throws RemoteException {
        // pass for now.
    }

    @Override
    public void crash(String arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public int getSupportedWakeLockFlags() throws RemoteException {
        // pass for now.
        return 0;
    }

    @Override
    public void goToSleep(long arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public void goToSleepWithReason(long arg0, int arg1) throws RemoteException {
        // pass for now.
    }

    @Override
    public void preventScreenOn(boolean arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public void reboot(String arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public void releaseWakeLock(IBinder arg0, int arg1) throws RemoteException {
        // pass for now.
    }

    @Override
    public void setAttentionLight(boolean arg0, int arg1) throws RemoteException {
        // pass for now.
    }

    @Override
    public void setAutoBrightnessAdjustment(float arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public void setBacklightBrightness(int arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public void setMaximumScreenOffTimeount(int arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public void setPokeLock(int arg0, IBinder arg1, String arg2) throws RemoteException {
        // pass for now.
    }

    @Override
    public void setStayOnSetting(int arg0) throws RemoteException {
        // pass for now.
    }

    @Override
    public void updateWakeLockWorkSource(IBinder arg0, WorkSource arg1) throws RemoteException {
        // pass for now.
    }

    @Override
    public void userActivity(long arg0, boolean arg1) throws RemoteException {
        // pass for now.
    }

    @Override
    public void userActivityWithForce(long arg0, boolean arg1, boolean arg2) throws RemoteException {
        // pass for now.
    }
}
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.layoutlib.bridge.util;

import com.android.resources.ResourceType;
import com.android.util.Pair;

import android.util.SparseArray;

import java.util.HashMap;
import java.util.Map;

public class DynamicIdMap {

    private final Map<Pair<ResourceType, String>, Integer> mDynamicIds = new HashMap<Pair<ResourceType, String>, Integer>();
    private final SparseArray<Pair<ResourceType, String>> mRevDynamicIds = new SparseArray<Pair<ResourceType, String>>();
    private int mDynamicSeed;

    public DynamicIdMap(int seed) {
        mDynamicSeed = seed;
    }

    public void reset(int seed) {
        mDynamicIds.clear();
        mRevDynamicIds.clear();
        mDynamicSeed = seed;
    }

    /**
     * Returns a dynamic integer for the given resource type/name, creating it if it doesn't
     * already exist.
     *
     * @param type the type of the resource
     * @param name the name of the resource
     * @return an integer.
     */
    public Integer getId(ResourceType type, String name) {
        return getId(Pair.of(type, name));
    }

    /**
     * Returns a dynamic integer for the given resource type/name, creating it if it doesn't
     * already exist.
     *
     * @param resource the type/name of the resource
     * @return an integer.
     */
    public Integer getId(Pair<ResourceType, String> resource) {
        Integer value = mDynamicIds.get(resource);
        if (value == null) {
            value = Integer.valueOf(++mDynamicSeed);
            mDynamicIds.put(resource, value);
            mRevDynamicIds.put(value, resource);
        }

        return value;
    }

    public Pair<ResourceType, String> resolveId(int id) {
        return mRevDynamicIds.get(id);
    }
}
Loading