Fix nullability issues.

This commit is contained in:
onurays 2020-03-26 17:19:29 +03:00
parent e5482d48c0
commit 0a77d5014e
21 changed files with 57 additions and 53 deletions

View file

@ -16,10 +16,8 @@
package im.vector.riotx.core.files
import android.app.DownloadManager
import android.content.ContentValues
import android.content.Context
import android.os.Build
import android.provider.MediaStore
import androidx.annotation.WorkerThread
import arrow.core.Try
@ -57,23 +55,17 @@ fun addEntryToDownloadManager(context: Context,
mimeType: String,
title: String = file.name,
description: String = file.name) {
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentValues = ContentValues().apply {
put(MediaStore.Downloads.TITLE, title)
put(MediaStore.Downloads.DISPLAY_NAME, description)
put(MediaStore.Downloads.MIME_TYPE, mimeType)
put(MediaStore.Downloads.SIZE, file.length())
val contentValues = ContentValues().apply {
put(MediaStore.Downloads.TITLE, title)
put(MediaStore.Downloads.DISPLAY_NAME, description)
put(MediaStore.Downloads.MIME_TYPE, mimeType)
put(MediaStore.Downloads.SIZE, file.length())
}
context.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)?.let { uri ->
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() })
}
context.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)?.let { uri ->
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() })
}
}
} else {
downloadManager?.addCompletedDownload(title, description, true, mimeType, file.absolutePath, file.length(), true)
}
} catch (e: Exception) {
Timber.e(e, "## addEntryToDownloadManager(): Exception")

View file

@ -95,7 +95,7 @@ class VectorGlideDataFetcher(private val activeSessionHolder: ActiveSessionHolde
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
Timber.v("Load data: $data")
if (data.isLocalFile()) {
if (data.isLocalFile() && data.url != null) {
val initialFile = File(data.url)
callback.onDataReady(FileInputStream(initialFile))
return

View file

@ -333,7 +333,7 @@ class EllipsizingTextView @JvmOverloads constructor(context: Context, attrs: Att
* @param workingText text to strip end punctuation from
* @return Text without end punctuation.
*/
fun stripEndPunctuation(workingText: CharSequence?): String {
fun stripEndPunctuation(workingText: CharSequence): String {
return mEndPunctPattern!!.matcher(workingText).replaceFirst("")
}
}

View file

@ -134,7 +134,7 @@ abstract class VectorBaseActivity : AppCompatActivity(), HasScreenInjector {
restorables.forEach { it.onSaveInstanceState(outState) }
}
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
restorables.forEach { it.onRestoreInstanceState(savedInstanceState) }
super.onRestoreInstanceState(savedInstanceState)
}

View file

@ -128,13 +128,13 @@ class CallService : VectorService() {
* Display a call in progress notification.
*/
private fun displayCallInProgressNotification(intent: Intent) {
val callId = intent.getStringExtra(EXTRA_CALL_ID)
val callId = intent.getStringExtra(EXTRA_CALL_ID) ?: ""
val notification = notificationUtils.buildPendingCallNotification(
intent.getBooleanExtra(EXTRA_IS_VIDEO, false),
intent.getStringExtra(EXTRA_ROOM_NAME),
intent.getStringExtra(EXTRA_ROOM_ID),
intent.getStringExtra(EXTRA_MATRIX_ID),
intent.getStringExtra(EXTRA_ROOM_NAME) ?: "",
intent.getStringExtra(EXTRA_ROOM_ID) ?: "",
intent.getStringExtra(EXTRA_MATRIX_ID) ?: "",
callId)
startForeground(NOTIFICATION_ID, notification)

View file

@ -37,9 +37,10 @@ class Debouncer(private val handler: Handler) {
fun cancel(identifier: String) {
if (runnables.containsKey(identifier)) {
val old = runnables[identifier]
handler.removeCallbacks(old)
runnables.remove(identifier)
runnables[identifier]?.let {
handler.removeCallbacks(it)
runnables.remove(identifier)
}
}
}

View file

@ -73,7 +73,7 @@ private fun logAction(file: File): Boolean {
*/
private fun recursiveActionOnFile(file: File, action: ActionOnFile): Boolean {
if (file.isDirectory) {
file.list().forEach {
file.list()?.forEach {
val result = recursiveActionOnFile(File(file, it), action)
if (!result) {

View file

@ -35,8 +35,8 @@ object TextUtils {
if (value < 1000) return value.toString() // deal with easy case
val e = suffixes.floorEntry(value)
val divideBy = e.key
val suffix = e.value
val divideBy = e?.key
val suffix = e?.value
val truncated = value / (divideBy!! / 10) // the number part of the output times 10
val hasDecimal = truncated < 100 && truncated / 10.0 != (truncated / 10).toDouble()

View file

@ -43,7 +43,7 @@ class AttachmentsPreviewActivity : VectorBaseActivity(), ToolbarConfigurable {
}
fun getOutput(intent: Intent): List<ContentAttachmentData> {
return intent.getParcelableArrayListExtra(ATTACHMENTS_PREVIEW_RESULT)
return intent.getParcelableArrayListExtra(ATTACHMENTS_PREVIEW_RESULT) ?: emptyList()
}
fun getKeepOriginalSize(intent: Intent): Boolean {

View file

@ -153,7 +153,7 @@ class KeysBackupSetupActivity : SimpleFragmentActivity() {
}
override fun onFailure(failure: Throwable) {
toast(failure.localizedMessage)
failure.localizedMessage?.let { toast(it) }
hideWaitingView()
}
})

View file

@ -158,8 +158,8 @@ class SharedSecureStorageViewModel @AssistedInject constructor(
@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: SharedSecureStorageViewState): SharedSecureStorageViewModel? {
val activity: SharedSecureStorageActivity = viewModelContext.activity()
val args: SharedSecureStorageActivity.Args = activity.intent.getParcelableExtra(MvRx.KEY_ARG)
return activity.viewModelFactory.create(state, args)
val args: SharedSecureStorageActivity.Args? = activity.intent.getParcelableExtra(MvRx.KEY_ARG)
return args?.let { activity.viewModelFactory.create(state, it) }
}
}
}

View file

@ -357,7 +357,9 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(
}
}
} catch (failure: Throwable) {
_viewEvents.post(VerificationBottomSheetViewEvents.ModalError(failure.localizedMessage))
failure.localizedMessage?.let {
_viewEvents.post(VerificationBottomSheetViewEvents.ModalError(it))
}
}
}
}.exhaustive

View file

@ -782,7 +782,7 @@ class RoomDetailFragment @Inject constructor(
updateComposerText("")
}
is RoomDetailViewEvents.SlashCommandResultError -> {
displayCommandError(sendMessageResult.throwable.localizedMessage)
sendMessageResult.throwable.localizedMessage?.let { displayCommandError(it) }
}
is RoomDetailViewEvents.SlashCommandNotImplemented -> {
displayCommandError(getString(R.string.not_implemented))

View file

@ -56,10 +56,17 @@ class ImageMediaViewerActivity : VectorBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(im.vector.riotx.R.layout.activity_image_media_viewer)
mediaData = intent.getParcelableExtra(EXTRA_MEDIA_DATA)
if (intent.hasExtra(EXTRA_MEDIA_DATA)) {
mediaData = intent.getParcelableExtra(EXTRA_MEDIA_DATA)!!
} else {
finish()
}
intent.extras?.getString(EXTRA_SHARED_TRANSITION_NAME)?.let {
ViewCompat.setTransitionName(imageTransitionView, it)
}
if (mediaData.url.isNullOrEmpty()) {
supportFinishAfterTransition()
return

View file

@ -37,11 +37,14 @@ class VideoMediaViewerActivity : VectorBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(im.vector.riotx.R.layout.activity_video_media_viewer)
val mediaData = intent.getParcelableExtra<VideoContentRenderer.Data>(EXTRA_MEDIA_DATA)
configureToolbar(videoMediaViewerToolbar, mediaData)
imageContentRenderer.render(mediaData.thumbnailMediaData, ImageContentRenderer.Mode.FULL_SIZE, videoMediaViewerThumbnailView)
videoContentRenderer.render(mediaData, videoMediaViewerThumbnailView, videoMediaViewerLoading, videoMediaViewerVideoView, videoMediaViewerErrorView)
if (intent.hasExtra(EXTRA_MEDIA_DATA)) {
val mediaData = intent.getParcelableExtra<VideoContentRenderer.Data>(EXTRA_MEDIA_DATA)!!
configureToolbar(videoMediaViewerToolbar, mediaData)
imageContentRenderer.render(mediaData.thumbnailMediaData, ImageContentRenderer.Mode.FULL_SIZE, videoMediaViewerThumbnailView)
videoContentRenderer.render(mediaData, videoMediaViewerThumbnailView, videoMediaViewerLoading, videoMediaViewerVideoView, videoMediaViewerErrorView)
}
}
private fun configureToolbar(toolbar: Toolbar, mediaData: VideoContentRenderer.Data) {

View file

@ -97,7 +97,7 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
val message = getReplyMessage(intent)
val roomId = intent.getStringExtra(KEY_ROOM_ID)
if (message.isNullOrBlank() || roomId.isBlank()) {
if (message.isNullOrBlank() || roomId.isNullOrBlank()) {
// ignore this event
// Can this happen? should we update notification?
return

View file

@ -65,7 +65,7 @@ class VectorFileLogger @Inject constructor(val context: Context, private val vec
sFileHandler?.formatter = LogFormatter()
sLogger.useParentHandlers = false
sLogger.level = Level.ALL
sLogger.addHandler(sFileHandler)
sFileHandler?.let { sLogger.addHandler(it) }
}
} catch (e: Throwable) {
Timber.e(e, "Failed to initialize FileLogger")

View file

@ -70,7 +70,7 @@ class RoomPreviewActivity : VectorBaseActivity(), ToolbarConfigurable {
if (isFirstCreation()) {
val args = intent.getParcelableExtra<RoomPreviewData>(ARG)
if (args.worldReadable) {
if (args?.worldReadable == true) {
// TODO Room preview: Note: M does not recommend to use /events anymore, so for now we just display the room preview
// TODO the same way if it was not world readable
addFragment(R.id.simpleFragmentContainer, RoomPreviewNoPreviewFragment::class.java, args)

View file

@ -59,9 +59,9 @@ object VectorLocale {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences.contains(APPLICATION_LOCALE_LANGUAGE_KEY)) {
applicationLocale = Locale(preferences.getString(APPLICATION_LOCALE_LANGUAGE_KEY, ""),
preferences.getString(APPLICATION_LOCALE_COUNTRY_KEY, ""),
preferences.getString(APPLICATION_LOCALE_VARIANT_KEY, "")
applicationLocale = Locale(preferences.getString(APPLICATION_LOCALE_LANGUAGE_KEY, "")!!,
preferences.getString(APPLICATION_LOCALE_COUNTRY_KEY, "")!!,
preferences.getString(APPLICATION_LOCALE_VARIANT_KEY, "")!!
)
} else {
applicationLocale = Locale.getDefault()

View file

@ -29,7 +29,6 @@ import im.vector.riotx.R
import im.vector.riotx.features.homeserver.ServerUrlsRepository
import im.vector.riotx.features.themes.ThemeUtils
import timber.log.Timber
import java.io.File
import javax.inject.Inject
class VectorPreferences @Inject constructor(private val context: Context) {
@ -68,7 +67,7 @@ class VectorPreferences @Inject constructor(private val context: Context) {
const val SETTINGS_ENCRYPTION_EXPORT_E2E_ROOM_KEYS_PREFERENCE_KEY = "SETTINGS_ENCRYPTION_EXPORT_E2E_ROOM_KEYS_PREFERENCE_KEY"
const val SETTINGS_ENCRYPTION_IMPORT_E2E_ROOM_KEYS_PREFERENCE_KEY = "SETTINGS_ENCRYPTION_IMPORT_E2E_ROOM_KEYS_PREFERENCE_KEY"
const val SETTINGS_ENCRYPTION_NEVER_SENT_TO_PREFERENCE_KEY = "SETTINGS_ENCRYPTION_NEVER_SENT_TO_PREFERENCE_KEY"
const val SETTINGS_SHOW_DEVICES_LIST_PREFERENCE_KEY = "SETTINGS_SHOW_DEVICES_LIST_PREFERENCE_KEY"
const val SETTINGS_SHOW_DEVICES_LIST_PREFERENCE_KEY = "SETTINGS_SHOW_DEVICES_LIST_PREFERENCE_KEY"
const val SETTINGS_SECURE_MESSAGE_RECOVERY_PREFERENCE_KEY = "SETTINGS_SECURE_MESSAGE_RECOVERY_PREFERENCE_KEY"
@ -427,11 +426,11 @@ class VectorPreferences @Inject constructor(private val context: Context) {
val toneUri = getNotificationRingTone() ?: return null
try {
val proj = arrayOf(MediaStore.Audio.Media.DATA)
val proj = arrayOf(MediaStore.Audio.Media.DISPLAY_NAME)
return context.contentResolver.query(toneUri, proj, null, null, null)?.use {
val columnIndex = it.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
val columnIndex = it.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)
it.moveToFirst()
File(it.getString(columnIndex)).nameWithoutExtension
it.getString(columnIndex)
}
} catch (e: Exception) {
Timber.e(e, "## getNotificationRingToneName() failed")

View file

@ -332,7 +332,7 @@ class VectorSettingsSecurityPrivacyFragment @Inject constructor(
}
override fun onFailure(failure: Throwable) {
appContext.toast(failure.localizedMessage)
failure.localizedMessage?.let { appContext.toast(it) }
hideLoadingView()
}
})