/* * Copyright 2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ @file:Suppress("NOTHING_TO_INLINE", "unused") package org.jetbrains.anko import android.annotation.SuppressLint import android.app.Activity import android.app.Service import android.content.Context import android.content.Intent import android.net.Uri import androidx.fragment.app.Fragment import org.jetbrains.anko.internals.AnkoInternals inline fun Context.startActivity(vararg params: Pair) = AnkoInternals.internalStartActivity(this, T::class.java, params) inline fun AnkoContext<*>.startActivity(vararg params: Pair) = AnkoInternals.internalStartActivity(ctx, T::class.java, params) inline fun Fragment.startActivity(vararg params: Pair) = AnkoInternals.internalStartActivity(requireActivity(), T::class.java, params) inline fun Activity.startActivityForResult( requestCode: Int, vararg params: Pair, ) = AnkoInternals.internalStartActivityForResult(this, T::class.java, requestCode, params) @Suppress("DEPRECATION") @Deprecated( message = "startActivityForResult is deprecated at Android 12", replaceWith = ReplaceWith("ActivityResult") ) inline fun Fragment.startActivityForResult( requestCode: Int, vararg params: Pair, ) = startActivityForResult( AnkoInternals.createIntent(requireActivity(), T::class.java, params), requestCode ) inline fun Context.startService(vararg params: Pair) = AnkoInternals.internalStartService(this, T::class.java, params) inline fun AnkoContext<*>.startService(vararg params: Pair) = AnkoInternals.internalStartService(ctx, T::class.java, params) inline fun Fragment.startService(vararg params: Pair) = AnkoInternals.internalStartService(requireActivity(), T::class.java, params) inline fun Context.stopService(vararg params: Pair) = AnkoInternals.internalStopService(this, T::class.java, params) inline fun AnkoContext<*>.stopService(vararg params: Pair) = AnkoInternals.internalStopService(ctx, T::class.java, params) inline fun Fragment.stopService(vararg params: Pair) = AnkoInternals.internalStopService(requireActivity(), T::class.java, params) inline fun Context.intentFor(vararg params: Pair): Intent = AnkoInternals.createIntent(this, T::class.java, params) inline fun AnkoContext<*>.intentFor(vararg params: Pair): Intent = AnkoInternals.createIntent(ctx, T::class.java, params) inline fun Fragment.intentFor(vararg params: Pair): Intent = AnkoInternals.createIntent(requireActivity(), T::class.java, params) /** * Add the [Intent.FLAG_ACTIVITY_CLEAR_TASK] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.clearTask(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) } /** * Add the [Intent.FLAG_ACTIVITY_CLEAR_TOP] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.clearTop(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) } /** * Add the [Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET] flag to the [Intent]. * * @return the same intent with the flag applied. */ @Deprecated( message = "Deprecated in Android", replaceWith = ReplaceWith("org.jetbrains.anko.newDocument") ) inline fun Intent.clearWhenTaskReset(): Intent = apply { @Suppress("DEPRECATION") addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) } /** * Add the [Intent.FLAG_ACTIVITY_NEW_DOCUMENT] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.newDocument(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT) } /** * Add the [Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.excludeFromRecents(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) } /** * Add the [Intent.FLAG_ACTIVITY_MULTIPLE_TASK] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.multipleTask(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK) } /** * Add the [Intent.FLAG_ACTIVITY_NEW_TASK] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.newTask(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } /** * Add the [Intent.FLAG_ACTIVITY_NO_ANIMATION] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.noAnimation(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION) } /** * Add the [Intent.FLAG_ACTIVITY_NO_HISTORY] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.noHistory(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) } /** * Add the [Intent.FLAG_ACTIVITY_SINGLE_TOP] flag to the [Intent]. * * @return the same intent with the flag applied. */ inline fun Intent.singleTop(): Intent = apply { addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) } inline fun AnkoContext<*>.browse(url: String, newTask: Boolean = false) = ctx.browse(url, newTask) inline fun Fragment.browse(url: String, newTask: Boolean = false) = requireActivity().browse(url, newTask) fun Context.browse(url: String, newTask: Boolean = false) { val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse(url) if (newTask) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } startActivity(intent) // ActivityNotFoundException } inline fun AnkoContext<*>.share(text: String, subject: String = "", title: String? = null) = ctx.share(text, subject, title) inline fun Fragment.share(text: String, subject: String = "", title: String? = null) = requireActivity().share(text, subject, title) fun Context.share(text: String, subject: String = "", title: String? = null) { val intent = Intent(Intent.ACTION_SEND) intent.type = "text/plain" intent.putExtra(Intent.EXTRA_SUBJECT, subject) intent.putExtra(Intent.EXTRA_TEXT, text) startActivity(Intent.createChooser(intent, title)) // ActivityNotFoundException } inline fun AnkoContext<*>.email(email: String, subject: String = "", text: String = "") = ctx.email(email, subject, text) inline fun Fragment.email(email: String, subject: String = "", text: String = "") = requireActivity().email(email, subject, text) @SuppressLint("QueryPermissionsNeeded") fun Context.email(email: String, subject: String = "", text: String = ""): Boolean { val intent = Intent(Intent.ACTION_SENDTO) intent.data = Uri.parse("mailto:") intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email)) if (subject.isNotEmpty()) intent.putExtra(Intent.EXTRA_SUBJECT, subject) if (text.isNotEmpty()) intent.putExtra(Intent.EXTRA_TEXT, text) if (intent.resolveActivity(packageManager) != null) { startActivity(intent) return true } return false } inline fun AnkoContext<*>.makeCall(number: String) = ctx.makeCall(number) inline fun Fragment.makeCall(number: String) = requireActivity().makeCall(number) fun Context.makeCall(number: String) { val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:$number")) startActivity(intent) // ActivityNotFoundException } inline fun AnkoContext<*>.sendSMS(number: String, text: String = "") = ctx.sendSMS(number, text) inline fun Fragment.sendSMS(number: String, text: String = "") = requireActivity().sendSMS(number, text) fun Context.sendSMS(number: String, text: String = "") { val intent = Intent(Intent.ACTION_VIEW, Uri.parse("sms:$number")) intent.putExtra("sms_body", text) startActivity(intent) // ActivityNotFoundException }