Skip to content

Instantly share code, notes, and snippets.

public class MyDialogFragment extends DialogFragment {
public interface DialogListener {
void onFinishDialog(String data);
}
public MyDialogFragment() {}
...
public class MainActivity extends FragmentActivity
implements MyDialogFragment.DialogListener {
...
public void showMyDialog() {
DialogFragment newFragment = new MyDialogFragment();
newFragment.show(getSupportFragmentManager(), "idStringFragment");
}
/**
* Enables TLS v1.2 when creating SSLSockets.
*
* For some reason, android supports TLS v1.2 from API 16, but enables it by
* default only from API 20.
* @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html
* @see SSLSocketFactory
*/
class Tls12SocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() {
fun OkHttpClient.Builder.enableTls12OnPreLollipop(): OkHttpClient.Builder {
if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) {
try {
val sc = SSLContext.getInstance("TLSv1.2")
sc.init(null, null, null)
this.sslSocketFactory(Tls12SocketFactory(sc.socketFactory))
val cs = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.build()
class LocationSettingDispatcher(activity: Activity) {
private val maybeSubscribe = UpdateLocationMaybeSubscribe(activity)
private val subject = PublishSubject.create<Boolean>()
companion object {
val REQUEST_CHECK_SETTINGS = 0x1
}
fun onActivityResult(requestCode: Int, resultCode: Int) {
class LocationDeviceInteractor(activity: Activity) {
private val provider = LocationServices.getFusedLocationProviderClient(activity)
private val settingDispatcher = LocationSettingDispatcher(activity)
private val lastLocationMaybe = Maybe.create<Location> { emitter ->
try {
provider.lastLocation.apply {
addOnSuccessListener { location ->
if (location != null) emitter.onSuccess(location)
@v170nix
v170nix / GooglePlayServicesAvailable.kt
Last active October 12, 2017 19:01
Android: Check whether Google Play Services are installed and current
@Singleton
class GooglePlayServicesAvailable @Inject constructor(private val context: Context) {
companion object {
private val PLAY_SERVICES_RESOLUTION_REQUEST = 4948
}
sealed class Result {
object True : Result()
object False : Result()
...
@Inject
internal lateinit var googlePlayServicesAvailable: GooglePlayServicesAvailable
...
override fun onCreate(savedInstanceState: Bundle?) {
...
googlePlayServicesAvailable.isAvailableShowDialog(this).let {
when (it) {
is GooglePlayServicesAvailable.Result.True -> RiseSetActivityPermissionsDispatcher.locationPermissionWithCheck(this)
@MustBeDocumented
@Target(AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
@Module
abstract class DemoModule {
@Binds
@IntoMap
@ViewModelKey(DemoViewModel::class)
internal abstract fun demoViewModel(viewModel: DemoViewModel): ViewModel
...