1 /* 2 * Copyright (C) 2014 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 17 package com.android.tv.settings; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.res.Resources; 22 23 import com.android.tv.settings.util.UriUtils; 24 25 /** 26 * Represents a Menu Item 27 */ 28 public class MenuItem { 29 30 public interface UriGetter { getUri()31 String getUri(); 32 } 33 34 public interface TextGetter { getText()35 String getText(); 36 } 37 38 public static class Builder { 39 private int mId; 40 private String mTitle; 41 private TextGetter mDescriptionGetter; 42 private UriGetter mImageUriGetter; 43 private Intent mIntent; 44 from(MenuItem item)45 public Builder from(MenuItem item) { 46 mId = item.mId; 47 mTitle = item.mDisplayName; 48 mDescriptionGetter = item.mDisplayDescriptionTextGetter; 49 mImageUriGetter = item.mImageUriGetter; 50 mIntent = item.mIntent; 51 return this; 52 } 53 id(int id)54 public Builder id(int id) { 55 mId = id; 56 return this; 57 } 58 title(String title)59 public Builder title(String title) { 60 mTitle = title; 61 return this; 62 } 63 descriptionGetter(TextGetter descriptionGetter)64 public Builder descriptionGetter(TextGetter descriptionGetter) { 65 mDescriptionGetter = descriptionGetter; 66 return this; 67 } 68 description(String description)69 public Builder description(String description) { 70 return descriptionGetter(new MenuItem.ConstantTextGetter(description)); 71 } 72 imageUriGetter(UriGetter imageUriGetter)73 public Builder imageUriGetter(UriGetter imageUriGetter) { 74 mImageUriGetter = imageUriGetter; 75 return this; 76 } 77 imageResourceId(Context context, int imageResourceId)78 public Builder imageResourceId(Context context, int imageResourceId) { 79 return imageResourceId(context.getResources(), imageResourceId); 80 } 81 imageResourceId(Resources resources, int imageResourceId)82 public Builder imageResourceId(Resources resources, int imageResourceId) { 83 return imageUri( 84 UriUtils.getAndroidResourceUri(resources, imageResourceId)); 85 } 86 imageUri(String uri)87 public Builder imageUri(String uri) { 88 return imageUriGetter(new MenuItem.ConstantUriGetter(uri)); 89 } 90 intent(Intent intent)91 public Builder intent(Intent intent) { 92 mIntent = intent; 93 return this; 94 } 95 build()96 public MenuItem build() { 97 return new MenuItem(mId, mTitle, mDescriptionGetter, mImageUriGetter, 98 mIntent); 99 } 100 } 101 102 private static class ConstantUriGetter implements MenuItem.UriGetter { 103 104 private final String mUri; 105 ConstantUriGetter(String uri)106 public ConstantUriGetter(String uri) { 107 mUri = uri; 108 } 109 110 @Override getUri()111 public String getUri() { 112 return mUri; 113 } 114 } 115 116 private static class ConstantTextGetter implements MenuItem.TextGetter { 117 118 private final String mText; 119 ConstantTextGetter(String text)120 public ConstantTextGetter(String text) { 121 mText = text; 122 } 123 124 @Override getText()125 public String getText() { 126 return mText; 127 } 128 } 129 130 private final int mId; 131 private final String mDisplayName; 132 private final TextGetter mDisplayDescriptionTextGetter; 133 private final UriGetter mImageUriGetter; 134 private final Intent mIntent; 135 MenuItem(int id, String displayName, TextGetter displayDescriptionTextGetter, UriGetter imageUriGetter, Intent intent)136 private MenuItem(int id, String displayName, 137 TextGetter displayDescriptionTextGetter, UriGetter imageUriGetter, 138 Intent intent) { 139 mId = id; 140 mDisplayName = displayName; 141 mDisplayDescriptionTextGetter = displayDescriptionTextGetter; 142 mImageUriGetter = imageUriGetter; 143 mIntent = intent; 144 } 145 getId()146 public int getId() { 147 return mId; 148 } 149 getTitle()150 public String getTitle() { 151 return mDisplayName; 152 } 153 getDescriptionGetter()154 public TextGetter getDescriptionGetter() { 155 return mDisplayDescriptionTextGetter; 156 } 157 getImageUriGetter()158 public UriGetter getImageUriGetter() { 159 return mImageUriGetter; 160 } 161 getIntent()162 public Intent getIntent() { 163 return mIntent; 164 } 165 } 166