summaryrefslogtreecommitdiffstats
path: root/remoting/android/java/src/org/chromium/chromoting/ChromotingUtil.java
blob: 3d2bb492ca91e424ded496cee4670be547076b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chromoting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;

import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Log;

/** Utility methods for chromoting code. */
public abstract class ChromotingUtil {
    private static final String TAG = "Chromoting";

    /**
     * Tints all icons of a toolbar menu so they have the same color as the 'back' navigation icon
     * and the three-dots overflow icon.
     * @param context Context for getting theme and resource information.
     * @param menu Menu with icons to be tinted.
     */
    public static void tintMenuIcons(Context context, Menu menu) {
        int color = getColorAttribute(context, R.attr.colorControlNormal);
        int items = menu.size();
        for (int i = 0; i < items; i++) {
            ChromotingUtil.tintMenuIcon(menu.getItem(i), color);
        }
    }

    /**
     * Sets a color filter on the specified MenuItem.
     * @param menuItem MenuItem to tint.
     * @param color Color to set on the menuItem.
     */
    public static void tintMenuIcon(MenuItem menuItem, int color) {
        Drawable icon = menuItem.getIcon();
        if (icon != null) {
            icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
        }
    }

    /**
     * Returns a color from a theme attribute.
     * @param context Context with resources to look up.
     * @param attribute Attribute such as R.attr.colorControlNormal.
     * @return Color value.
     * @throws Resources.NotFoundException
     */
    public static int getColorAttribute(Context context, int attribute) {
        TypedValue typedValue = new TypedValue();
        if (!context.getTheme().resolveAttribute(attribute, typedValue, true)) {
            throw new Resources.NotFoundException("Attribute not found.");
        }

        if (typedValue.resourceId != 0) {
            // Attribute is a resource.
            return ApiCompatibilityUtils.getColor(context.getResources(), typedValue.resourceId);
        } else if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT
                && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
            // Attribute is a raw color value.
            return typedValue.data;
        } else {
            throw new Resources.NotFoundException("Attribute not a color.");
        }
    }

    /**
     * Starts a new Activity only if the system can resolve the given Intent. Useful for implicit
     * intents where the system might not have an application that can handle it.
     * @param context The parent context.
     * @param intent The (implicit) intent to launch.
     * @return True if the intent was resolved.
     */
    public static boolean startActivitySafely(Context context, Intent intent) {
        if (intent.resolveActivity(context.getPackageManager()) == null) {
            Log.w(TAG, "Unable to resolve activity for: %s", intent);
            return false;
        }
        context.startActivity(intent);
        return true;
    }

    /** Launches an external web browser or application. */
    public static boolean openUrl(Activity parentActivity, Uri uri) {
        return startActivitySafely(parentActivity, new Intent(Intent.ACTION_VIEW, uri));
    }
}