QFieldActivity.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * QFieldActivity.java - class needed to copy files from assets to getExternalFilesDir() before starting QtActivity
  3. * this can be used to perform actions before QtActivity takes over.
  4. * @author Marco Bernasocchi - <marco@opengis.ch>
  5. * @version 0.5
  6. */
  7. /*
  8. Copyright (c) 2011, Marco Bernasocchi <marco@opengis.ch>
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the Marco Bernasocchi <marco@opengis.ch> nor the
  18. names of its contributors may be used to endorse or promote products
  19. derived from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY Marco Bernasocchi <marco@opengis.ch> ''AS IS'' AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. DISCLAIMED. IN NO EVENT SHALL Marco Bernasocchi <marco@opengis.ch> BE LIABLE FOR ANY
  24. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. package ch.opengis.vrahelper;
  32. import java.io.BufferedInputStream;
  33. import java.io.File;
  34. import java.io.FileOutputStream;
  35. import java.io.FileNotFoundException;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.lang.Thread;
  39. import java.util.List;
  40. import java.util.ArrayList;
  41. import java.util.zip.ZipEntry;
  42. import java.util.zip.ZipInputStream;
  43. import android.app.Activity;
  44. import android.app.Application;
  45. import android.content.Context;
  46. import android.content.Intent;
  47. import android.content.pm.ActivityInfo;
  48. import android.content.pm.PackageManager;
  49. import android.content.pm.PackageManager.NameNotFoundException;
  50. import android.Manifest;
  51. import android.net.Uri;
  52. import android.os.AsyncTask;
  53. import android.os.Bundle;
  54. import android.os.Environment;
  55. import android.support.v4.app.ActivityCompat;
  56. import android.support.v4.content.ContextCompat;
  57. import android.view.WindowManager;
  58. import android.view.WindowManager.LayoutParams;
  59. import org.qtproject.qt5.android.bindings.QtActivity;
  60. import ch.opengis.vrahelper.R;
  61. import ch.opengis.vrahelper.QFieldUtils;
  62. public class QFieldActivity extends QtActivity {
  63. public static native void openProject(String url);
  64. private float originalBrightness;
  65. @Override
  66. public void onCreate(Bundle savedInstanceState) {
  67. prepareQtActivity();
  68. super.onCreate(savedInstanceState);
  69. }
  70. @Override
  71. public void onNewIntent(Intent intent) {
  72. super.onNewIntent(intent);
  73. if (intent.getAction() == Intent.ACTION_VIEW) {
  74. Uri uri = intent.getData();
  75. Context context = getApplication().getApplicationContext();
  76. openProject(QFieldUtils.getPathFromUri(context, uri));
  77. }
  78. }
  79. private void dimBrightness() {
  80. WindowManager.LayoutParams lp = getWindow().getAttributes();
  81. originalBrightness = lp.screenBrightness;
  82. lp.screenBrightness = 0.01f;
  83. getWindow().setAttributes(lp);
  84. }
  85. private void restoreBrightness() {
  86. WindowManager.LayoutParams lp = getWindow().getAttributes();
  87. lp.screenBrightness = originalBrightness;
  88. getWindow().setAttributes(lp);
  89. }
  90. private void prepareQtActivity() {
  91. checkPermissions();
  92. String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath();
  93. String qFieldDir = storagePath + "/QField/";
  94. new File(qFieldDir).mkdir();
  95. // create directories
  96. new File(qFieldDir + "basemaps/").mkdir();
  97. new File(qFieldDir + "fonts/").mkdir();
  98. new File(qFieldDir + "proj/").mkdir();
  99. new File(qFieldDir + "auth/").mkdir();
  100. Intent intent = new Intent();
  101. intent.setClass(QFieldActivity.this, QtActivity.class);
  102. try {
  103. ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
  104. intent.putExtra("GIT_REV", activityInfo.metaData.getString("android.app.git_rev"));
  105. } catch (NameNotFoundException e) {
  106. e.printStackTrace();
  107. finish();
  108. return;
  109. }
  110. intent.putExtra("QFIELD_DATA_DIR", qFieldDir);
  111. Intent sourceIntent = getIntent();
  112. if (sourceIntent.getAction() == Intent.ACTION_VIEW) {
  113. Uri uri = sourceIntent.getData();
  114. Context context = getApplication().getApplicationContext();
  115. intent.putExtra("QGS_PROJECT", QFieldUtils.getPathFromUri(context, uri));
  116. }
  117. setIntent(intent);
  118. }
  119. private void checkPermissions()
  120. {
  121. List<String> permissionsList = new ArrayList<String>();
  122. if (ContextCompat.checkSelfPermission(QFieldActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
  123. permissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
  124. }
  125. if (ContextCompat.checkSelfPermission(QFieldActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
  126. permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
  127. }
  128. if (ContextCompat.checkSelfPermission(QFieldActivity.this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_DENIED) {
  129. permissionsList.add(Manifest.permission.BLUETOOTH);
  130. }
  131. if (ContextCompat.checkSelfPermission(QFieldActivity.this, Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_DENIED) {
  132. permissionsList.add(Manifest.permission.BLUETOOTH_ADMIN);
  133. }
  134. if ( permissionsList.size() > 0 ) {
  135. String[] permissions = new String[ permissionsList.size() ];
  136. permissionsList.toArray( permissions );
  137. ActivityCompat.requestPermissions(QFieldActivity.this, permissions, 101);
  138. }
  139. }
  140. }