165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Intent;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.util.UriUtils;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Represents a Menu Item
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class MenuItem {
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public interface UriGetter {
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String getUri();
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public interface TextGetter {
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String getText();
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static class Builder {
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private int mId;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private String mTitle;
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private TextGetter mDescriptionGetter;
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private UriGetter mImageUriGetter;
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private Intent mIntent;
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder id(int id) {
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mId = id;
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return this;
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder title(String title) {
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mTitle = title;
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return this;
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder descriptionGetter(TextGetter descriptionGetter) {
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mDescriptionGetter = descriptionGetter;
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return this;
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder description(String description) {
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return descriptionGetter(new MenuItem.ConstantTextGetter(description));
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder imageUriGetter(UriGetter imageUriGetter) {
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mImageUriGetter = imageUriGetter;
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return this;
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder imageResourceId(Context context, int imageResourceId) {
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return imageUri(
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    UriUtils.getAndroidResourceUri(context.getResources(), imageResourceId));
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder imageUri(String uri) {
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return imageUriGetter(new MenuItem.ConstantUriGetter(uri));
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public Builder intent(Intent intent) {
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIntent = intent;
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return this;
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public MenuItem build() {
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return new MenuItem(mId, mTitle, mDescriptionGetter, mImageUriGetter,
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mIntent);
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static class ConstantUriGetter implements MenuItem.UriGetter {
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final String mUri;
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public ConstantUriGetter(String uri) {
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mUri = uri;
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public String getUri() {
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mUri;
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static class ConstantTextGetter implements MenuItem.TextGetter {
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final String mText;
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public ConstantTextGetter(String text) {
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mText = text;
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public String getText() {
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mText;
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final int mId;
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final String mDisplayName;
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final TextGetter mDisplayDescriptionTextGetter;
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final UriGetter mImageUriGetter;
12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final Intent mIntent;
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private MenuItem(int id, String displayName,
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            TextGetter displayDescriptionTextGetter, UriGetter imageUriGetter,
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Intent intent) {
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mId = id;
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mDisplayName = displayName;
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mDisplayDescriptionTextGetter = displayDescriptionTextGetter;
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mImageUriGetter = imageUriGetter;
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mIntent = intent;
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public long getId() {
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mId;
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public String getTitle() {
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mDisplayName;
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public TextGetter getDescriptionGetter() {
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mDisplayDescriptionTextGetter;
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public UriGetter getImageUriGetter() {
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mImageUriGetter;
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public Intent getIntent() {
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mIntent;
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
152