Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BasePermissionsDialogFragment.java « permissions « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e2958a1f1926e402d2459704d75ac3b6a684b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.mapswithme.maps.permissions;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmDialogFragment;
import com.mapswithme.util.PermissionsUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

abstract class BasePermissionsDialogFragment extends BaseMwmDialogFragment
    implements View.OnClickListener
{
  private static final String TAG = BasePermissionsDialogFragment.class.getName();
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  private static final String ARG_REQUEST_CODE = "arg_request_code";

  private int mRequestCode;

  @SuppressWarnings("TryWithIdenticalCatches")
  @Nullable
  public static DialogFragment show(@NonNull FragmentActivity activity, int requestCode,
                                    @NonNull Class<? extends BaseMwmDialogFragment> dialogClass)
  {
    final FragmentManager fm = activity.getSupportFragmentManager();
    if (fm.isDestroyed())
      return null;

    Fragment f = fm.findFragmentByTag(dialogClass.getName());
    if (f != null)
      return (DialogFragment) f;

    BaseMwmDialogFragment dialog = null;
    try
    {
      dialog = dialogClass.newInstance();
      final Bundle args = new Bundle();
      args.putInt(ARG_REQUEST_CODE, requestCode);
      dialog.setArguments(args);
      dialog.show(fm, dialogClass.getName());
    }
    catch (java.lang.InstantiationException e)
    {
      LOGGER.e(TAG, "Can't instantiate " + dialogClass.getName() + " fragment", e);
    }
    catch (IllegalAccessException e)
    {
      LOGGER.e(TAG, "Can't instantiate " + dialogClass.getName() + " fragment", e);
    }

    return dialog;
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    Bundle args = getArguments();
    if (args != null)
      mRequestCode = args.getInt(ARG_REQUEST_CODE);
  }

  @Override
  protected int getCustomTheme()
  {
    // We can't read actual theme, because permissions are not granted yet.
    return R.style.MwmTheme_DialogFragment_Fullscreen;
  }

  @NonNull
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState)
  {
    Dialog res = super.onCreateDialog(savedInstanceState);
    res.requestWindowFeature(Window.FEATURE_NO_TITLE);

    View content = View.inflate(getActivity(), getLayoutRes(), null);
    res.setContentView(content);
    View button = content.findViewById(getFirstActionButton());
    if (button != null)
      button.setOnClickListener(this);
    button = content.findViewById(getContinueActionButton());
    if (button != null)
      button.setOnClickListener(this);

    ImageView image = (ImageView) content.findViewById(R.id.iv__image);
    if (image != null)
      image.setImageResource(getImageRes());
    TextView title = (TextView) content.findViewById(R.id.tv__title);
    if (title != null)
      title.setText(getTitleRes());
    TextView subtitle = (TextView) content.findViewById(R.id.tv__subtitle1);
    if (subtitle != null)
      subtitle.setText(getSubtitleRes());

    return res;
  }

  @DrawableRes
  protected int getImageRes()
  {
    return 0;
  }

  @StringRes
  protected int getTitleRes()
  {
    return 0;
  }

  @StringRes
  protected int getSubtitleRes()
  {
    return 0;
  }

  @LayoutRes
  abstract protected int getLayoutRes();

  @IdRes
  protected abstract int getFirstActionButton();

  protected abstract void onFirstActionClick();

  @IdRes
  protected abstract int getContinueActionButton();

  @Override
  public void onClick(@NonNull View v)
  {
    if (v.getId() == getFirstActionButton())
    {
      onFirstActionClick();
      return;
    }

    if (v.getId() == getContinueActionButton())
      PermissionsUtils.requestPermissions(getActivity(), mRequestCode);
  }

  protected int getRequestCode()
  {
    return mRequestCode;
  }
}