Posts

Showing posts from 2020

How to store encryption keys safely in Android 19+?

We have to encrypt our data while saving or sending over the Internet. We have to use shared key algorithms for encryption because they are fast. But what to do with the keys, how to generate and keep them safe? Developers usually use some of the approaches mentioned below: 1. Generate shared key in the app with shared logic in app and server. This can be used for encrypting/decrypting data locally and for sending data over Internet. Problem with this approach is, app can be decompiled and logic can reconstructed. Once the logic is reconstructed hacker can keep making the keys as and when required. 2. Get key from Server and use it. Security of the key depends on how the key is transported to Mobile app. If someone can grab it, then it is compromised and can be used to decrypt data and can even be used to modify it.  Recommended approach for this is to use HTTPS connection and send key on it. Ideally new key should be used with each request, as this gives very little time to the ha...

Curious case of MissingMethodInvocationException and Mockito.when

Have you tried to mock a method in Kotlin? As mentioned in this guide we can simply use following code to make it work. //You can mock concrete classes, not just interfaces  LinkedList mockedList = mock(LinkedList.class); / /stubbing  Mockito.when(mockedList.get(0)).thenReturn("first"); If you try it on kotlin class, it will fail with exception MissingMethodInvocationException. e.g. open class DummyPresenter { fun getSomeValue():String{ return "Some Value" } } @RunWith (MockitoJUnitRunner:: class ) class ExampleUnitTest { @Test fun testCallGetSomeValue() { val presenter: DummyPresenter = mock(DummyPresenter:: class . java ) `when`(presenter.getSomeValue()).thenReturn( "Some other value" ) val result = TestClass().callGetSomeValue(presenter) println ( "Test Result: $ result " ) assertEquals( "Some other value" ,result) } } org.mockito.exceptions.misusing.MissingMethodInvocationException:  when() requi...

Flutter: How to get device details like UUID, deviceName?

We will use  device_info package for finding these details. Use this link to check latest version of the package.  Add  package_info : ^0.4.0+3 in pubspec.yaml file of your project. below  dependencies :    flutter :      sdk : flutter     package_info : ^0.4.0+3 Using next code sample you can retrieve the details. Future<List<String>> getDeviceDetails() async {     String deviceName;     String deviceVersion;     String identifier;     final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();     try {       if (Platform.isAndroid) {                  var build = await deviceInfoPlugin.androidInfo;         deviceName = build.model;         deviceVersion = build.version.toString();         identifier = build.androidId; //UUID for Android   ...

New to Flutter? Good resources to begin with.

Coming from Android background https://flutter.dev/docs/get-started/flutter-for/android-devs https://codelabs.developers.google.com/codelabs/from-java-to-dart/#0 Coming from iOS background https://flutter.dev/docs/get-started/flutter-for/ios-devs Find out which widget to choose: https://flutter.dev/docs/development/ui/widgets/material https://flutter.dev/docs/development/ui/layout https://flutter.dev/docs/development/ui/widgets How to use Assets? https://flutter.dev/docs/development/ui/assets-and-images Create flavours? https://flutter.dev/docs/deployment/flavors Building widget based on api Response https://medium.com/nonstopio/flutter-future-builder-with-list-view-builder-d7212314e8c9 Building widget events of stream https://codingwithjoe.com/flutter-building-a-widget-with-streambuilder/

Using AndroidViewModel for accessing application context

If you want to use context in view model, you can't use ViewModel as  it does not provide you context needed for creating Database. So Android architecture components introduced AndroidViewModel. If we use ViewModelProvider(this).get(ProductListingVM::class. java ), it will cause error "java.lang.Class<com.example.swapnil.sat.models.ProductListingVM> has no zero argument constructor" thrown at runtime. So, how to use it? We shall pass AndroidViewModelFactory instance while creating the ViewModelProvider.  Let's have a look. MainActivity.kt import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.ViewModelProvider class MainActivity : AppCompatActivity() { lateinit var viewModel : ProductListingVM override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout. activity_main ) viewModel =ViewModelProvider( this , ViewModelProvider.Andro...