8367f835047e57c70a508975d020905f0173f51aTom Rumsey/*
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * The contents of this file are subject to the terms of the Common Development and
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Distribution License (the License). You may not use this file except in compliance with the
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * License.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey *
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * specific language governing permission and limitations under the License.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey *
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * When distributing Covered Software, include this CDDL Header Notice in each file and include
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Header, with the fields enclosed by brackets [] replaced by your own identifying
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * information: "Portions copyright [year] [name of copyright owner]".
8367f835047e57c70a508975d020905f0173f51aTom Rumsey *
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Copyright 2016 ForgeRock AS.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey */
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumseypackage com.forgerock.authenticator.ui;
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.content.Context;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.os.Bundle;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.util.AttributeSet;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.view.LayoutInflater;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.view.View;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.view.ViewGroup;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.widget.FrameLayout;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.widget.ImageView;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.widget.RelativeLayout;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport android.widget.TextView;
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport com.forgerock.authenticator.R;
8367f835047e57c70a508975d020905f0173f51aTom Rumseyimport com.forgerock.authenticator.mechanisms.base.Mechanism;
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumseyimport com.forgerock.authenticator.notifications.Notification;
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumsey/**
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * UI element which is an icon representation of a Mechanism. Contains both the icon for the
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Mechanism, as well as a badge which displays the number of notifications attached to that
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Mechanism.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey */
8367f835047e57c70a508975d020905f0173f51aTom Rumseypublic class MechanismIcon extends FrameLayout {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumsey /**
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Create an icon that a will represent a Mechanism.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * @param context The context that the view is created from.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey */
8367f835047e57c70a508975d020905f0173f51aTom Rumsey public MechanismIcon(Context context) {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey super(context);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey init();
8367f835047e57c70a508975d020905f0173f51aTom Rumsey }
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumsey /**
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Create an icon that a will represent a Mechanism.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * @param context The context that the view is created from.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * @param attrs The list of attributes.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey */
8367f835047e57c70a508975d020905f0173f51aTom Rumsey public MechanismIcon(Context context, AttributeSet attrs) {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey super(context, attrs);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey init();
8367f835047e57c70a508975d020905f0173f51aTom Rumsey }
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumsey /**
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Create an icon that a will represent a Mechanism.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * @param context The context that the view is created from.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * @param attrs The list of attributes.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * @param defStyleAttr The resource containing default style attributes.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey */
8367f835047e57c70a508975d020905f0173f51aTom Rumsey public MechanismIcon(Context context, AttributeSet attrs, int defStyleAttr) {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey super(context, attrs, defStyleAttr);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey init();
8367f835047e57c70a508975d020905f0173f51aTom Rumsey }
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumsey private void init() {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey inflate(getContext(), R.layout.mechanismicon, this);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey }
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumsey /**
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * Set the Mechanism that this icon represents.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey * @param mechanism The Mechanism that is represented by this icon.
8367f835047e57c70a508975d020905f0173f51aTom Rumsey */
8367f835047e57c70a508975d020905f0173f51aTom Rumsey public void setMechanism(Mechanism mechanism) {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey ImageView icon = (ImageView) findViewById(R.id.icon_image);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey icon.setImageDrawable(getResources().getDrawable(mechanism.getInfo().getIcon()));
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey int activeNotifications = 0;
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey for (Notification notification : mechanism.getNotifications()) {
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey if (notification.isActive()) {
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey activeNotifications++;
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey }
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey }
c00a5587d32883c0f366fd6c47c3e674dad9dba7Tom Rumsey setNotificationNumber(activeNotifications);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey }
8367f835047e57c70a508975d020905f0173f51aTom Rumsey
8367f835047e57c70a508975d020905f0173f51aTom Rumsey private void setNotificationNumber(int notificationNumber) {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey TextView badge = ((TextView) findViewById(R.id.badge));
8367f835047e57c70a508975d020905f0173f51aTom Rumsey if (notificationNumber == 0) {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey badge.setVisibility(GONE);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey } else {
8367f835047e57c70a508975d020905f0173f51aTom Rumsey badge.setVisibility(VISIBLE);
8367f835047e57c70a508975d020905f0173f51aTom Rumsey badge.setText(Integer.toString(notificationNumber));
8367f835047e57c70a508975d020905f0173f51aTom Rumsey }
8367f835047e57c70a508975d020905f0173f51aTom Rumsey }
8367f835047e57c70a508975d020905f0173f51aTom Rumsey}