Skip to content

Commit 8dd5bc1

Browse files
committed
---1--- Added NEARBY_WIFI_DEVICES permission for wifi_sd (android 13) ----2---- Shifted the atdReport Excel file's location to internal storage which can be accessed using file provider and granted URI read permission while opening it
1 parent 9a178d4 commit 8dd5bc1

19 files changed

Lines changed: 529 additions & 273 deletions

.idea/misc.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ plugins {
1212
}
1313

1414
android {
15-
compileSdk 32
15+
compileSdk 33
1616

1717
defaultConfig {
1818
applicationId "com.example.teachjr"
1919
minSdk 21
20-
targetSdk 32
20+
targetSdk 33
2121
versionCode 1
2222
versionName "1.0"
2323

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.example.teachjr">
55

6-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
7+
tools:ignore="ScopedStorage" />
78
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
8-
<!-- TODO: Implement permission stuff For android 11-->
9-
<!-- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>-->
109

1110
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
1211

@@ -24,7 +23,13 @@
2423
android:name="android.permission.ACCESS_COARSE_LOCATION" />
2524
<uses-permission
2625
android:required="true"
27-
android:name="android.permission.ACCESS_FINE_LOCATION" />
26+
android:name="android.permission.ACCESS_FINE_LOCATION"
27+
android:maxSdkVersion="32"/>
28+
29+
<!-- If your app targets Android 13 (API level 33)
30+
or higher, you must declare the NEARBY_WIFI_DEVICES permission. -->
31+
<uses-permission
32+
android:name="android.permission.NEARBY_WIFI_DEVICES" />
2833

2934
<application
3035
android:name=".di.MyApplication"

app/src/main/java/com/example/teachjr/ui/SplashActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class SplashActivity : AppCompatActivity() {
3333
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
3434

3535
Permissions.requestPermission(this@SplashActivity)
36+
Permissions.requestStoragePermissions(this@SplashActivity)
3637

3738
splashViewModel.userType.observe(this) {
3839
when(it) {

app/src/main/java/com/example/teachjr/ui/professor/profFragments/ProfAtdReportFragment.kt

Lines changed: 4 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,18 @@ class ProfAtdReportFragment : Fragment() {
3939
private val TAG = ProfAtdReportFragment::class.java.simpleName
4040
private lateinit var binding: FragmentProfAtdReportBinding
4141

42-
private lateinit var downloadFolderFile: File
43-
4442
private val atdReportViewModel by activityViewModels<ProfAtdReportViewModel>()
4543
private val sharedProfViewModel by activityViewModels<SharedProfViewModel>()
4644

4745
private lateinit var viewPagerAdapter: AtdReportViewPagerAdapter
4846
private val tabLayoutTitles = arrayListOf("Lectures", "Students")
4947

50-
private lateinit var confirmDialogDownload: AlertDialog
51-
private lateinit var confirmDialogOpenUpload: AlertDialog
52-
53-
private val permissionRequestLauncher =
54-
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
55-
val granted = permissions.entries.all {
56-
it.value == true
57-
}
58-
if(granted) {
59-
openExcelFile()
60-
} else {
61-
Toast.makeText(context, "not GRANTED", Toast.LENGTH_SHORT).show()
62-
}
63-
}
64-
6548
override fun onCreateView(
6649
inflater: LayoutInflater, container: ViewGroup?,
6750
savedInstanceState: Bundle?
6851
): View {
6952
binding = FragmentProfAtdReportBinding.inflate(layoutInflater)
7053

71-
downloadFolderFile = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!
72-
// downloadFolderFile = requireContext().filesDir
73-
7454
return binding.root
7555
}
7656

@@ -86,9 +66,7 @@ class ProfAtdReportFragment : Fragment() {
8666
}
8767

8868
setupOptionsMenu()
89-
createConfirmDownloadDialog()
90-
createConfirmDialogOpenUpload()
91-
setupObservers()
69+
// setupObservers()
9270

9371

9472
viewPagerAdapter = AtdReportViewPagerAdapter(this)
@@ -113,115 +91,15 @@ class ProfAtdReportFragment : Fragment() {
11391
when(it.itemId) {
11492
R.id.action_download_report -> {
11593
if(atdReportViewModel.detailsLoaded) {
116-
checkPermissionAndShowDialog()
94+
findNavController().navigate(R.id.action_profAtdReportFragment_to_profExcelSheetFragment)
95+
// checkPermissionAndShowDialog()
11796
} else {
118-
Toast.makeText(context, "Data not available", Toast.LENGTH_SHORT).show()
97+
Toast.makeText(context, "Fetching data, please wait...", Toast.LENGTH_SHORT).show()
11998
}
12099
true
121100
}
122101
else -> false
123102
}
124103
}
125104
}
126-
127-
private fun setupObservers() {
128-
129-
atdReportViewModel.atdReportSaveStatus.observe(viewLifecycleOwner) {
130-
when(it) {
131-
is Response.Loading -> { showSaving() }
132-
is Response.Error -> {
133-
stopSaving()
134-
Toast.makeText(context, it.errorMessage, Toast.LENGTH_SHORT).show()
135-
}
136-
is Response.Success -> {
137-
stopSaving()
138-
confirmDialogOpenUpload.show()
139-
binding.layoutFileLocation.visibility = View.VISIBLE
140-
binding.tvFileLocation.text = downloadFolderFile.path.toString()
141-
}
142-
}
143-
}
144-
}
145-
146-
private fun checkPermissionAndShowDialog() {
147-
if(Permissions.hasReadStoragePermissions(activity as Context)
148-
&& Permissions.hasWriteStoragePermissions(activity as Context)) {
149-
150-
confirmDialogDownload.show()
151-
} else {
152-
permissionRequestLauncher.launch(Permissions.getPendingStoragePermissions(activity as Activity))
153-
}
154-
}
155-
156-
private fun createConfirmDownloadDialog() {
157-
confirmDialogDownload = AlertDialog.Builder(context)
158-
.setTitle("Convert to excel?")
159-
.setMessage("Attendance report will be saved as excel sheet")
160-
.setPositiveButton(Constants.YES) { _, _ ->
161-
162-
lifecycleScope.launch(Dispatchers.IO) {
163-
atdReportViewModel.downloadExcelSheet(downloadFolderFile.path.toString())
164-
}
165-
}
166-
.setNegativeButton(Constants.NO, null)
167-
.create()
168-
}
169-
170-
private fun createConfirmDialogOpenUpload() {
171-
confirmDialogOpenUpload = AlertDialog.Builder(context)
172-
.setTitle("File Saved")
173-
// .setMessage("Do you want to open it or upload it to google drive?")
174-
.setMessage("Do you want to open it?")
175-
.setPositiveButton("Open") { _, _ ->
176-
177-
openExcelFile()
178-
}
179-
// .setNegativeButton("Upload") {_,_ -> }
180-
// .setNeutralButton("Cancel", null)
181-
.setNegativeButton("Cancel", null)
182-
.create()
183-
}
184-
185-
private fun showSaving() {
186-
binding.viewPager.alpha = 0.5F
187-
binding.cvSaving.visibility = View.VISIBLE
188-
}
189-
190-
private fun stopSaving() {
191-
binding.viewPager.alpha = 1F
192-
binding.cvSaving.visibility = View.GONE
193-
}
194-
195-
private fun openExcelFile() {
196-
try {
197-
if(atdReportViewModel.sheetName != null) {
198-
val filePath = File(downloadFolderFile, "")
199-
val sheet = File(filePath, atdReportViewModel.sheetName!!)
200-
val contentUri = FileProvider.getUriForFile(
201-
requireContext(),
202-
"com.example.teachjr.fileProvider",
203-
sheet
204-
)
205-
Log.i(TAG, "TESTING, URI: ${contentUri.toString()}")
206-
207-
val intent = Intent(Intent.ACTION_VIEW)
208-
if(contentUri.toString().contains(".xls") || contentUri.toString().contains(".xlsx")) {
209-
intent.setDataAndType(contentUri, "application/vnd.ms-excel")
210-
}
211-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
212-
startActivity(intent)
213-
214-
// val file = File(atdReportViewModel.filePath)
215-
// OpenExcel.openFile(requireContext(), file)
216-
}
217-
} catch (e: ActivityNotFoundException) {
218-
// binding.tvNoActivityToHandleIntent.text = binding.tvNoActivityToHandleIntent.text.toString() + downloadFolderFile.path.toString()
219-
// binding.tvNoActivityToHandleIntent.visibility = View.VISIBLE
220-
Toast.makeText(context, "You don't have any app that can open an EXCEL file", Toast.LENGTH_LONG).show()
221-
}
222-
catch (e: Exception) {
223-
e.printStackTrace()
224-
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show()
225-
}
226-
}
227105
}

0 commit comments

Comments
 (0)