summaryrefslogtreecommitdiffstats
path: root/android/xscreensaver/src/org/jwz/xscreensaver/Activity.java
blob: ac0ab4c10b13cc9a713a8594897dda6ce5ca54db (plain) (blame)
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
161
162
163
164
165
166
167
168
169
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * xscreensaver, Copyright (c) 2016 Jamie Zawinski <jwz@jwz.org>
 * and Dennis Sheil <dennis@panaceasupplies.com>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 *
 * This is the XScreenSaver "application" that just brings up the
 * Live Wallpaper preferences.
 */

package org.jwz.xscreensaver;

import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.provider.Settings;
import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.os.Build;
import android.content.pm.PackageManager;

public class Activity extends android.app.Activity
  implements View.OnClickListener {

  private boolean wallpaperButtonClicked, daydreamButtonClicked;
  private final static int MY_REQ_READ_EXTERNAL_STORAGE = 271828;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // openList();
    setContentView(R.layout.activity_xscreensaver);
    wallpaperButtonClicked = false;
    daydreamButtonClicked = false;

    findViewById(R.id.apply_wallpaper).setOnClickListener(this);
    findViewById(R.id.apply_daydream).setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.apply_wallpaper:
      wallpaperButtonClicked();
      break;
    case R.id.apply_daydream:
      daydreamButtonClicked();
      break;
    }
  }

  // synchronized when dealing with wallpaper state - perhaps can
  // narrow down more
  private synchronized void withProceed() {
    if (daydreamButtonClicked) {
      String action;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        action = Settings.ACTION_DREAM_SETTINGS;
      } else {
        action = Settings.ACTION_DISPLAY_SETTINGS;
      }
      startActivity(new Intent(action));
    } else if (wallpaperButtonClicked) {
      startActivity(new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER));
    }
  }

  private void wallpaperButtonClicked() {
      wallpaperButtonClicked = true;
      checkPermission();
  }

  private void daydreamButtonClicked() {
      daydreamButtonClicked = true;
      checkPermission();
  }

  void checkPermission() {
      // RES introduced in API 16
      String permission = Manifest.permission.READ_EXTERNAL_STORAGE;
      if (havePermission(permission)) {
          withProceed();
      } else {
          noPermission(permission);
      }
  }

  private void noPermission(String permission) {
      int myRequestCode;
      myRequestCode = MY_REQ_READ_EXTERNAL_STORAGE;

      if (permissionsDeniedRationale(permission)) {
          showDeniedRationale();
      } else {
          requestPermission(permission, myRequestCode);
      }
  }

  private boolean permissionsDeniedRationale(String permission) {
      boolean rationale = ActivityCompat.shouldShowRequestPermissionRationale(this,
                permission);
      return rationale;
  }

  private void requestPermission(String permission, int myRequestCode) {
      ActivityCompat.requestPermissions(this,
              new String[]{permission},
              myRequestCode);

      // myRequestCode is an app-defined int constant.
      // The callback method gets the result of the request.
  }

  // TODO: This method should be asynchronous, and not block the thread
  private void showDeniedRationale() {
    withProceed();
  }

  boolean havePermission(String permission) {

      if (Build.VERSION.SDK_INT < 16) {
          return true;
      }

      if (permissionGranted(permission)) {
          return true;
      }

      return false;
  }

  private boolean permissionGranted(String permission) {
        boolean check = ContextCompat.checkSelfPermission(this, permission) ==
                PackageManager.PERMISSION_GRANTED;
      return check;
  }

  public void proceedIfPermissionGranted(int[] grantResults) {

      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
              && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          withProceed();
      } else if (grantResults.length > 0) {
          withProceed();
      }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
      case MY_REQ_READ_EXTERNAL_STORAGE:
          proceedIfPermissionGranted(grantResults);
    }
  }

}