1/*
2 * Copyright (C) 2017 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
17package com.android.dialer.simulator.impl;
18
19import android.content.Context;
20import android.support.annotation.NonNull;
21import android.support.annotation.Nullable;
22import android.view.ActionProvider;
23import android.view.MenuItem;
24import android.view.SubMenu;
25import android.view.View;
26import com.android.dialer.common.Assert;
27import java.util.ArrayList;
28import java.util.List;
29
30/** Makes it easier to create submenus in the simulator. */
31final class SimulatorSubMenu extends ActionProvider {
32  List<Item> items = new ArrayList<>();
33
34  SimulatorSubMenu(@NonNull Context context) {
35    super(Assert.isNotNull(context));
36  }
37
38  SimulatorSubMenu addItem(@NonNull String title, @NonNull Runnable clickHandler) {
39    items.add(new Item(title, clickHandler));
40    return this;
41  }
42
43  SimulatorSubMenu addItem(@NonNull String title, @NonNull ActionProvider actionProvider) {
44    items.add(new Item(title, actionProvider));
45    return this;
46  }
47
48  @Override
49  public View onCreateActionView() {
50    return null;
51  }
52
53  @Override
54  public View onCreateActionView(MenuItem forItem) {
55    return null;
56  }
57
58  @Override
59  public boolean hasSubMenu() {
60    return true;
61  }
62
63  @Override
64  public void onPrepareSubMenu(SubMenu subMenu) {
65    super.onPrepareSubMenu(subMenu);
66    subMenu.clear();
67
68    for (Item item : items) {
69      if (item.clickHandler != null) {
70        subMenu
71            .add(item.title)
72            .setOnMenuItemClickListener(
73                (i) -> {
74                  item.clickHandler.run();
75                  return true;
76                });
77      } else {
78        subMenu.add(item.title).setActionProvider(item.actionProvider);
79      }
80    }
81  }
82
83  private static final class Item {
84    @NonNull final String title;
85    @Nullable final Runnable clickHandler;
86    @Nullable final ActionProvider actionProvider;
87
88    Item(@NonNull String title, @NonNull Runnable clickHandler) {
89      this.title = Assert.isNotNull(title);
90      this.clickHandler = Assert.isNotNull(clickHandler);
91      actionProvider = null;
92    }
93
94    Item(@NonNull String title, @NonNull ActionProvider actionProvider) {
95      this.title = Assert.isNotNull(title);
96      this.clickHandler = null;
97      this.actionProvider = Assert.isNotNull(actionProvider);
98    }
99  }
100}
101