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.googlecode.android_scripting.facade.ui;
18
19import android.app.Dialog;
20
21import com.googlecode.android_scripting.facade.EventFacade;
22import com.googlecode.android_scripting.future.FutureActivityTask;
23
24import java.util.concurrent.CountDownLatch;
25
26abstract class DialogTask extends FutureActivityTask<Object> {
27
28  protected Dialog mDialog;
29  private EventFacade mEventFacade;
30
31  public EventFacade getEventFacade() {
32    return mEventFacade;
33  }
34
35  public void setEventFacade(EventFacade mEventFacade) {
36    this.mEventFacade = mEventFacade;
37  }
38
39  @Override
40  protected void setResult(Object object) {
41    super.setResult(object);
42    EventFacade eventFacade = getEventFacade();
43    if (eventFacade != null) {
44      eventFacade.postEvent("dialog", object);
45    }
46  }
47
48  protected final CountDownLatch mShowLatch = new CountDownLatch(1);
49
50  /**
51   * Returns the wrapped {@link Dialog}.
52   */
53  public Dialog getDialog() {
54    return mDialog;
55  }
56
57  /**
58   * Dismiss the {@link Dialog} and close {@link Sl4aActivity}.
59   */
60  public void dismissDialog() {
61    if (mDialog != null) {
62      mDialog.dismiss();
63      finish();
64    }
65    mDialog = null;
66  }
67
68  /**
69   * Returns the {@link CountDownLatch} that is counted down when the dialog is shown.
70   */
71  public CountDownLatch getShowLatch() {
72    return mShowLatch;
73  }
74}
75