├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── gif │ ├── room.gif │ ├── rxjava2.gif │ └── splash.gif ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── mahmoud │ │ └── samples │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── mahmoud │ │ │ ├── caching │ │ │ ├── AppDB.java │ │ │ ├── Converters.java │ │ │ ├── Injection.java │ │ │ ├── RoomPlayground.java │ │ │ ├── UserViewModel.java │ │ │ ├── ViewModelFactory.java │ │ │ ├── dao │ │ │ │ └── UserDao.java │ │ │ ├── datasource │ │ │ │ ├── LocalUserDataSource.java │ │ │ │ └── UserDataSource.java │ │ │ └── entity │ │ │ │ ├── Reminder.java │ │ │ │ ├── Task.java │ │ │ │ └── User.java │ │ │ ├── location │ │ │ └── LastKnownLocationActivity.java │ │ │ ├── networking │ │ │ ├── ApiClient.java │ │ │ ├── NetworkingActivity.java │ │ │ ├── NetworkingRequestState.java │ │ │ ├── adapter │ │ │ │ └── RepoAdapter.java │ │ │ ├── endpoint │ │ │ │ └── GithubReposApi.java │ │ │ ├── model │ │ │ │ └── Repo.java │ │ │ └── state │ │ │ │ ├── ApiRequest.java │ │ │ │ └── RequestState.java │ │ │ ├── samples │ │ │ ├── AppInfo.java │ │ │ ├── AppRichInfo.java │ │ │ ├── AppsAdapter.java │ │ │ ├── Utils.java │ │ │ ├── eighthexample │ │ │ │ └── MapOPeratorSample.java │ │ │ ├── firstexample │ │ │ │ └── MainActivity.java │ │ │ ├── fivethexample │ │ │ │ └── DistinctOperatorSample.java │ │ │ ├── fouthexample │ │ │ │ └── TakeOperatorSample.java │ │ │ ├── ninthexample │ │ │ │ └── ScanOPeratorSample.java │ │ │ ├── secondexample │ │ │ │ └── FormOperatorSample.java │ │ │ ├── seventhexample │ │ │ │ └── SkipOperatorSample.java │ │ │ ├── sixthexample │ │ │ │ └── FirstOperatorSample.java │ │ │ └── thirdexample │ │ │ │ └── FilterOPeratorSample.java │ │ │ └── timing │ │ │ ├── SplashScreen.java │ │ │ └── TimingActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_lastknown_location.xml │ │ ├── activity_main.xml │ │ ├── activity_networking.xml │ │ ├── activity_room_playground.xml │ │ ├── activity_rx_downloader.xml │ │ ├── activity_splash.xml │ │ ├── activity_timing.xml │ │ ├── item.xml │ │ └── repo_item.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── mahmoud │ └── samples │ └── ExampleUnitTest.java ├── build.gradle ├── contributing.md ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 40 | 1.8 41 | 42 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |

RxJavaSamples

8 | This repo is a container for some samples using RxJava2.This repo have the following packages 9 | 10 | Samples: This package has example that shows installed apps in your device 11 | in list with name and image for each app.This package have many activities 12 | each one has operator concept. 13 | 14 | Timing: This package has implementation for splash screen using Timer operator and also another 15 | activity that shows the using of timer and interval operators 16 | 17 | Networking: This package has example shows my repos on github on list with rety option for failed request 18 | and Request state Like IDLE,LOADING,ERROR,COMPLETED using BehaviorSubject 19 | 20 | Caching: This package has example shows my bio update using Room data base with RxJava 21 | 22 |

How to run the samples

23 | I used the package style per feature so for instance you will find under networking package 24 | all related activities with awesome concepts.So to test certain concept add the activity refrence 25 | in AndroidManifest.xml 26 | 27 |

28 | 29 | 30 | 31 | 32 |

33 | 34 | 35 |

Used Libraries

36 | 44 | 45 |

Contributing

46 | Any contributions are more than welcomed from other developers to help us make the 47 | Repo even better. Before you contribute there are a number of things that you should 48 | know please see Contributing for details. 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.example.mahmoud.rxinstalledapps" 7 | minSdkVersion 16 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | compileOptions { 21 | sourceCompatibility JavaVersion.VERSION_1_8 22 | targetCompatibility JavaVersion.VERSION_1_8 23 | } 24 | } 25 | 26 | def supportLibVersion = "26.1.0" 27 | def butterKnifeVersion = "8.8.1" 28 | def okhttpVersion = "3.6.0" 29 | dependencies { 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | implementation "com.android.support:appcompat-v7:$supportLibVersion" 32 | implementation "com.android.support:recyclerview-v7:$supportLibVersion" 33 | implementation "com.android.support:cardview-v7:$supportLibVersion" 34 | implementation "com.squareup.okhttp3:okhttp:$okhttpVersion" 35 | 36 | implementation "com.jakewharton:butterknife:$butterKnifeVersion" 37 | annotationProcessor "com.jakewharton:butterknife-compiler:$butterKnifeVersion" 38 | //Rxjava2 39 | implementation 'io.reactivex.rxjava2:rxjava:2.1.7' 40 | implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' 41 | 42 | implementation 'com.github.lzyzsd:circleprogress:1.2.1' 43 | 44 | //retrofit 45 | implementation 'com.squareup.retrofit2:retrofit:2.3.0' 46 | //Gson 47 | implementation 'com.google.code.gson:gson:2.8.2' 48 | //Gson-Converter 49 | implementation 'com.squareup.retrofit2:converter-gson:2.3.0' 50 | //okhttp logging 51 | compile 'com.squareup.okhttp3:logging-interceptor:3.8.0' 52 | // rxjava2 adaptar 53 | compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' 54 | 55 | //room lib 56 | implementation "android.arch.persistence.room:runtime:1.0.0" 57 | annotationProcessor "android.arch.persistence.room:compiler:1.0.0" 58 | implementation 'android.arch.persistence.room:rxjava2:1.0.0' 59 | 60 | //arch components 61 | implementation "android.arch.lifecycle:extensions:1.0.0" 62 | annotationProcessor "android.arch.lifecycle:compiler:1.0.0" 63 | 64 | //location 65 | compile 'com.google.android.gms:play-services-location:12.0.0' 66 | //design lib 67 | implementation "com.android.support:design:$supportLibVersion" 68 | 69 | testImplementation 'junit:junit:4.12' 70 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 71 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 72 | 73 | } 74 | -------------------------------------------------------------------------------- /app/gif/room.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrabelwahed/RxjavaSamples/67058671d35381b87818924748dc34fc68352822/app/gif/room.gif -------------------------------------------------------------------------------- /app/gif/rxjava2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrabelwahed/RxjavaSamples/67058671d35381b87818924748dc34fc68352822/app/gif/rxjava2.gif -------------------------------------------------------------------------------- /app/gif/splash.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrabelwahed/RxjavaSamples/67058671d35381b87818924748dc34fc68352822/app/gif/splash.gif -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/mahmoud/samples/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.mahmoud.rxinstalledapps", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/AppDB.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching; 2 | 3 | import android.arch.persistence.room.Database; 4 | import android.arch.persistence.room.Room; 5 | import android.arch.persistence.room.RoomDatabase; 6 | import android.content.Context; 7 | 8 | import com.example.mahmoud.caching.dao.UserDao; 9 | import com.example.mahmoud.caching.entity.User; 10 | 11 | /** 12 | * Created by mahmoud on 27/03/18. 13 | */ 14 | 15 | @Database(entities = {User.class}, version = 1) 16 | public abstract class AppDB extends RoomDatabase { 17 | 18 | private static final String DATABASE_NAME = "sample.db"; 19 | private static volatile AppDB INSTANCE; 20 | 21 | public static AppDB getInstance(Context context){ 22 | if (INSTANCE == null){ 23 | synchronized (AppDB.class){ 24 | if (INSTANCE == null) { 25 | INSTANCE = Room.databaseBuilder(context.getApplicationContext(), 26 | AppDB.class, DATABASE_NAME) 27 | .build(); 28 | } 29 | } 30 | 31 | } 32 | return INSTANCE; 33 | } 34 | 35 | public abstract UserDao userDao(); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/Converters.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching; 2 | 3 | import android.arch.persistence.room.TypeConverter; 4 | 5 | import java.util.Date; 6 | 7 | /** 8 | * Created by mahmoud on 27/03/18. 9 | */ 10 | 11 | public class Converters { 12 | 13 | @TypeConverter 14 | public static Long fromDateToTimeStamp(Date date){ 15 | return date==null? null:date.getTime(); 16 | } 17 | 18 | 19 | @TypeConverter 20 | public static Date fromTimeToDate(Long time){ 21 | return time==null? null: new Date(time); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/Injection.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching; 2 | 3 | import android.content.Context; 4 | 5 | import com.example.mahmoud.caching.datasource.LocalUserDataSource; 6 | import com.example.mahmoud.caching.datasource.UserDataSource; 7 | 8 | /** 9 | * Created by mahmoud on 04/04/18. 10 | */ 11 | 12 | public class Injection { 13 | public static UserDataSource provideUserDataSource(Context context){ 14 | AppDB appDB = AppDB.getInstance(context); 15 | return new LocalUserDataSource(appDB.userDao()); 16 | } 17 | 18 | public static ViewModelFactory provideViewModelFactory(Context context){ 19 | UserDataSource userDataSource = provideUserDataSource(context); 20 | return new ViewModelFactory(userDataSource); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/RoomPlayground.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching; 2 | 3 | import android.arch.lifecycle.ViewModelProviders; 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.util.Log; 9 | import android.widget.Button; 10 | import android.widget.EditText; 11 | import android.widget.TextView; 12 | 13 | import com.example.mahmoud.samples.R; 14 | 15 | import butterknife.BindView; 16 | import butterknife.ButterKnife; 17 | import butterknife.OnClick; 18 | import io.reactivex.android.schedulers.AndroidSchedulers; 19 | import io.reactivex.disposables.CompositeDisposable; 20 | import io.reactivex.schedulers.Schedulers; 21 | 22 | /** 23 | * Created by mahmoud on 27/03/18. 24 | */ 25 | 26 | public class RoomPlayground extends AppCompatActivity { 27 | 28 | @BindView(R.id.user_name) 29 | TextView usernameTV; 30 | @BindView(R.id.user_name_input) 31 | EditText usernameInput; 32 | @BindView(R.id.user_bio_input) 33 | EditText userBioInput; 34 | @BindView(R.id.update_user) 35 | Button updateBtn; 36 | 37 | 38 | private ViewModelFactory mViewModelFactory; 39 | 40 | private UserViewModel mViewModel; 41 | 42 | private final CompositeDisposable mDisposable = new CompositeDisposable(); 43 | 44 | private AppDB appDB; 45 | 46 | @Override 47 | protected void onCreate(@Nullable Bundle savedInstanceState) { 48 | super.onCreate(savedInstanceState); 49 | setContentView(R.layout.activity_room_playground); 50 | ButterKnife.bind(this); 51 | 52 | mViewModelFactory = Injection.provideViewModelFactory(this); 53 | mViewModel = ViewModelProviders.of(this, mViewModelFactory) 54 | .get(UserViewModel.class); 55 | 56 | 57 | } 58 | 59 | @Override 60 | protected void onStart() { 61 | super.onStart(); 62 | mDisposable.add(mViewModel.getUser() 63 | .subscribeOn(Schedulers.io()) 64 | .observeOn(AndroidSchedulers.mainThread()) 65 | .subscribe(user -> usernameTV.setText(user.getUsername()+"\n"+user.getBio()), 66 | throwable -> Log.d("error", throwable.getMessage()))); 67 | } 68 | 69 | 70 | @Override 71 | protected void onStop() { 72 | super.onStop(); 73 | mDisposable.clear(); 74 | } 75 | 76 | @OnClick(R.id.update_user) 77 | public void onUpdateUserNameClicked() { 78 | String usrInput = usernameInput.getText().toString(); 79 | String bioInput = userBioInput.getText().toString(); 80 | updateBtn.setEnabled(false); 81 | updateBtn.setTextColor(Color.parseColor("#f7f7f7")); 82 | mDisposable.add(mViewModel.updateUserName(usrInput,bioInput) 83 | .subscribeOn(Schedulers.io()) 84 | .observeOn(AndroidSchedulers.mainThread()) 85 | .subscribe(() -> { 86 | updateBtn.setEnabled(true); 87 | updateBtn.setTextColor(Color.parseColor("#000000")); 88 | })); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/UserViewModel.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching; 2 | 3 | import android.arch.lifecycle.ViewModel; 4 | 5 | import com.example.mahmoud.caching.datasource.UserDataSource; 6 | import com.example.mahmoud.caching.entity.User; 7 | 8 | import io.reactivex.Completable; 9 | import io.reactivex.Flowable; 10 | 11 | /** 12 | * Created by mahmoud on 04/04/18. 13 | */ 14 | 15 | public class UserViewModel extends ViewModel { 16 | 17 | private UserDataSource mUserDataSource; 18 | private User mUser; 19 | 20 | public UserViewModel(UserDataSource mUserDataSource ){ 21 | this.mUserDataSource = mUserDataSource; 22 | } 23 | 24 | public Flowable getUser(){ 25 | return mUserDataSource.getUser() 26 | .map(user ->{ 27 | mUser=user; 28 | return mUser; 29 | }); 30 | } 31 | 32 | public Completable updateUserName(String username,String bio){ 33 | return Completable.fromAction(()->{ 34 | mUser = (mUser==null)? new User(username):new User(mUser.getId(),username,bio); 35 | mUserDataSource.insertOrUpdate(mUser); 36 | }); 37 | } 38 | 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/ViewModelFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching; 2 | 3 | import android.arch.lifecycle.ViewModel; 4 | import android.arch.lifecycle.ViewModelProvider; 5 | import android.support.annotation.NonNull; 6 | 7 | import com.example.mahmoud.caching.datasource.UserDataSource; 8 | 9 | /** 10 | * Created by mahmoud on 04/04/18. 11 | */ 12 | 13 | public class ViewModelFactory implements ViewModelProvider.Factory { 14 | private final UserDataSource mDataSource; 15 | 16 | public ViewModelFactory(UserDataSource dataSource){ 17 | mDataSource = dataSource; 18 | } 19 | 20 | @NonNull 21 | @Override 22 | public T create(@NonNull Class modelClass) { 23 | if (modelClass.isAssignableFrom(UserViewModel.class)){ 24 | return (T) new UserViewModel(mDataSource); 25 | } 26 | throw new IllegalArgumentException("unknown view model"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching.dao; 2 | 3 | import android.arch.persistence.room.Dao; 4 | import android.arch.persistence.room.Insert; 5 | import android.arch.persistence.room.OnConflictStrategy; 6 | import android.arch.persistence.room.Query; 7 | import android.arch.persistence.room.Update; 8 | 9 | import com.example.mahmoud.caching.entity.User; 10 | 11 | import io.reactivex.Flowable; 12 | 13 | /** 14 | * Created by mahmoud on 27/03/18. 15 | */ 16 | 17 | @Dao 18 | public interface UserDao { 19 | @Insert(onConflict = OnConflictStrategy.REPLACE) 20 | void insert(User user); 21 | 22 | @Query("DELETE FROM Users") 23 | void deleteAllUsers(); 24 | 25 | @Update(onConflict = OnConflictStrategy.REPLACE) 26 | void update(User user); 27 | 28 | @Query("SELECT * FROM Users LIMIT 1") 29 | Flowable getUser(); 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/datasource/LocalUserDataSource.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching.datasource; 2 | 3 | import com.example.mahmoud.caching.dao.UserDao; 4 | import com.example.mahmoud.caching.entity.User; 5 | 6 | import io.reactivex.Flowable; 7 | 8 | /** 9 | * Created by mahmoud on 04/04/18. 10 | */ 11 | 12 | public class LocalUserDataSource implements UserDataSource { 13 | private final UserDao userDao; 14 | 15 | public LocalUserDataSource(UserDao userDao) { 16 | this.userDao = userDao; 17 | } 18 | 19 | @Override 20 | public Flowable getUser() { 21 | return userDao.getUser(); 22 | } 23 | 24 | @Override 25 | public void insertOrUpdate(User user) { 26 | userDao.insert(user); 27 | } 28 | 29 | @Override 30 | public void deleteAllUsers() { 31 | userDao.deleteAllUsers(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/datasource/UserDataSource.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching.datasource; 2 | 3 | import com.example.mahmoud.caching.entity.User; 4 | 5 | import io.reactivex.Flowable; 6 | 7 | /** 8 | * Created by mahmoud on 04/04/18. 9 | */ 10 | 11 | public interface UserDataSource { 12 | Flowable getUser(); 13 | 14 | void insertOrUpdate(User user); 15 | 16 | void deleteAllUsers(); 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/entity/Reminder.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching.entity; 2 | 3 | import android.arch.persistence.room.ColumnInfo; 4 | import android.arch.persistence.room.Entity; 5 | import android.arch.persistence.room.ForeignKey; 6 | import android.arch.persistence.room.PrimaryKey; 7 | import android.arch.persistence.room.TypeConverters; 8 | 9 | import com.example.mahmoud.caching.Converters; 10 | 11 | import java.util.Date; 12 | 13 | /** 14 | * Created by mahmoud on 27/03/18. 15 | */ 16 | 17 | @Entity(foreignKeys = @ForeignKey( 18 | entity = Task.class, 19 | childColumns ="taskId", 20 | parentColumns = "id", 21 | onDelete = ForeignKey.CASCADE)) 22 | @TypeConverters(Converters.class) 23 | public class Reminder { 24 | 25 | @PrimaryKey(autoGenerate = true) 26 | @ColumnInfo(name = "reminderId") 27 | private long id; 28 | private long taskId; 29 | private Date date; 30 | 31 | public long getId() { 32 | return id; 33 | } 34 | 35 | public void setId(long id) { 36 | this.id = id; 37 | } 38 | 39 | public long getTaskId() { 40 | return taskId; 41 | } 42 | 43 | public void setTaskId(long taskId) { 44 | this.taskId = taskId; 45 | } 46 | 47 | public Date getDate() { 48 | return date; 49 | } 50 | 51 | public void setDate(Date date) { 52 | this.date = date; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/entity/Task.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching.entity; 2 | 3 | import android.arch.persistence.room.Entity; 4 | import android.arch.persistence.room.ForeignKey; 5 | import android.arch.persistence.room.PrimaryKey; 6 | 7 | /** 8 | * Created by mahmoud on 27/03/18. 9 | */ 10 | 11 | //delete child objects of parent is deleted 12 | @Entity(foreignKeys = @ForeignKey(entity = User.class, 13 | childColumns = "userId", 14 | parentColumns = "id", 15 | onDelete = ForeignKey.CASCADE)) 16 | public class Task { 17 | 18 | @PrimaryKey(autoGenerate = true) 19 | private long id; 20 | private long userId; 21 | private String name; 22 | 23 | public long getId() { 24 | return id; 25 | } 26 | 27 | public void setId(long id) { 28 | this.id = id; 29 | } 30 | 31 | public long getUserId() { 32 | return userId; 33 | } 34 | 35 | public void setUserId(long userId) { 36 | this.userId = userId; 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | public void setName(String name) { 44 | this.name = name; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/caching/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.caching.entity; 2 | 3 | import android.arch.persistence.room.ColumnInfo; 4 | import android.arch.persistence.room.Entity; 5 | import android.arch.persistence.room.Ignore; 6 | import android.arch.persistence.room.PrimaryKey; 7 | import android.support.annotation.NonNull; 8 | 9 | import java.util.UUID; 10 | 11 | /** 12 | * Created by mahmoud on 27/03/18. 13 | */ 14 | 15 | @Entity(tableName = "users") 16 | public class User { 17 | 18 | 19 | @NonNull 20 | @ColumnInfo(name = "user_id") 21 | @PrimaryKey 22 | private String id; 23 | 24 | @ColumnInfo(name = "user_name") 25 | private String username; 26 | 27 | @ColumnInfo(name = "bio") 28 | private String bio; 29 | 30 | @Ignore 31 | public User(String username) { 32 | this.id = UUID.randomUUID().toString(); 33 | this.username = username; 34 | } 35 | 36 | public String getBio() { 37 | return bio; 38 | } 39 | 40 | public void setBio(String bio) { 41 | this.bio = bio; 42 | } 43 | 44 | public User(String id, String username , String bio) { 45 | this.id = id; 46 | this.username = username; 47 | 48 | this.bio = bio; 49 | } 50 | 51 | public String getUsername() { 52 | return username; 53 | } 54 | 55 | public void setUsername(String username) { 56 | this.username = username; 57 | } 58 | 59 | public String getId() { 60 | return id; 61 | } 62 | 63 | public void setId(String id) { 64 | this.id = id; 65 | } 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/location/LastKnownLocationActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.location; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.content.pm.PackageManager; 6 | import android.location.Location; 7 | import android.net.Uri; 8 | import android.os.Bundle; 9 | import android.provider.Settings; 10 | import android.support.annotation.NonNull; 11 | import android.support.annotation.Nullable; 12 | import android.support.design.widget.Snackbar; 13 | import android.support.v4.app.ActivityCompat; 14 | import android.support.v7.app.AppCompatActivity; 15 | import android.view.View; 16 | import android.widget.LinearLayout; 17 | import android.widget.TextView; 18 | 19 | import com.example.mahmoud.samples.BuildConfig; 20 | import com.example.mahmoud.samples.R; 21 | import com.google.android.gms.location.FusedLocationProviderClient; 22 | import com.google.android.gms.location.LocationServices; 23 | 24 | import butterknife.BindView; 25 | import butterknife.ButterKnife; 26 | 27 | 28 | /** 29 | * Created by mahmoud on 28/03/18. 30 | */ 31 | 32 | public class LastKnownLocationActivity extends AppCompatActivity { 33 | @BindView(R.id.lat) 34 | TextView latText; 35 | @BindView(R.id.lng) 36 | TextView lngText; 37 | @BindView(R.id.main_activity_container) 38 | LinearLayout parent; 39 | 40 | private FusedLocationProviderClient fusedLocClien; 41 | private static final int LOCATION_PERMISSION_REQUEST = 0x01; 42 | private Location mLastLocation; 43 | @Override 44 | protected void onCreate(@Nullable Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_lastknown_location); 47 | ButterKnife.bind(this); 48 | fusedLocClien = LocationServices.getFusedLocationProviderClient(this); 49 | } 50 | 51 | @Override 52 | protected void onStart() { 53 | super.onStart(); 54 | if (!checkPermission()) { 55 | requestPermission(); 56 | } else { 57 | getLastLocation(); 58 | } 59 | } 60 | 61 | @Override 62 | protected void onPause() { 63 | super.onPause(); 64 | } 65 | 66 | private void requestPermission() { 67 | boolean shouldProvideRational = ActivityCompat. 68 | shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION); 69 | // Provide an additional rationale to the user. This would happen if the user denied the 70 | // request previously, but didn't check the "Don't ask again" checkbox. 71 | if (shouldProvideRational) { 72 | showSnackBar(R.string.permission_request,R.string.ok,v->{ 73 | startLocationPermissionRequest(); 74 | }); 75 | } else { 76 | // Request permission. It's possible this can be auto answered if device policy 77 | // sets the permission in a given state or the user denied the permission 78 | // previously and checked "Never ask again". 79 | startLocationPermissionRequest(); 80 | } 81 | 82 | } 83 | 84 | private void startLocationPermissionRequest(){ 85 | ActivityCompat.requestPermissions(this, 86 | new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 87 | LOCATION_PERMISSION_REQUEST); 88 | } 89 | 90 | private boolean checkPermission() { 91 | int permissionState = ActivityCompat.checkSelfPermission(this, 92 | Manifest.permission.ACCESS_COARSE_LOCATION); 93 | return permissionState == PackageManager.PERMISSION_GRANTED; 94 | } 95 | 96 | @SuppressWarnings("MissingPermission") 97 | private void getLastLocation() { 98 | fusedLocClien.getLastLocation() 99 | .addOnSuccessListener(this, location -> { 100 | if (location!= null) { 101 | mLastLocation = location; 102 | 103 | latText.setText(String.valueOf(mLastLocation.getLatitude())); 104 | lngText.setText(String.valueOf(mLastLocation.getLongitude())); 105 | } else { 106 | showSnackbar 107 | (getString(R.string.no_location_detected)); 108 | } 109 | }); 110 | } 111 | private void showSnackbar(final String text) { 112 | if (parent != null) { 113 | Snackbar.make(parent, text, Snackbar.LENGTH_LONG).show(); 114 | } 115 | } 116 | 117 | private void showSnackBar(int mainString, int actionStringId, View.OnClickListener listener){ 118 | Snackbar.make(findViewById(android.R.id.content), 119 | getString(mainString), 120 | Snackbar.LENGTH_INDEFINITE).setAction(getString(actionStringId),listener); 121 | } 122 | 123 | @Override 124 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 125 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 126 | if (requestCode == LOCATION_PERMISSION_REQUEST && grantResults.length>0){ 127 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED){ 128 | startLocationPermissionRequest(); 129 | }else{ 130 | showSnackBar(R.string.permission_denied_explanation, R.string.settings, 131 | new View.OnClickListener() { 132 | @Override 133 | public void onClick(View view) { 134 | // Build intent that displays the App settings screen. 135 | Intent intent = new Intent(); 136 | intent.setAction( 137 | Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 138 | Uri uri = Uri.fromParts("package", 139 | BuildConfig.APPLICATION_ID, null); 140 | intent.setData(uri); 141 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 142 | startActivity(intent); 143 | } 144 | }); 145 | } 146 | } 147 | } 148 | } 149 | 150 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/ApiClient.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking; 2 | 3 | import com.example.mahmoud.networking.endpoint.GithubReposApi; 4 | import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; 5 | 6 | import okhttp3.OkHttpClient; 7 | import okhttp3.logging.HttpLoggingInterceptor; 8 | import retrofit2.Retrofit; 9 | import retrofit2.converter.gson.GsonConverterFactory; 10 | 11 | /** 12 | * Created by mahmoud on 20/03/18. 13 | */ 14 | 15 | public class ApiClient { 16 | private static ApiClient instance; 17 | private static final String BASE_URL="https://api.github.com/"; 18 | private Retrofit retrofit; 19 | 20 | private ApiClient (){ 21 | buildRetrofit(); 22 | } 23 | 24 | public synchronized static ApiClient getInstance(){ 25 | if (instance == null) 26 | instance = new ApiClient(); 27 | return instance; 28 | } 29 | 30 | private Retrofit buildRetrofit(){ 31 | OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder(); 32 | //log url body 33 | HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); 34 | loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 35 | okHttpBuilder.addInterceptor(loggingInterceptor); 36 | 37 | retrofit = new Retrofit.Builder() 38 | .baseUrl(BASE_URL) 39 | .addConverterFactory(GsonConverterFactory.create()) 40 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 41 | .client(okHttpBuilder.build()) 42 | .build(); 43 | 44 | return retrofit; 45 | } 46 | 47 | public GithubReposApi getGithubReposApi(){ 48 | return retrofit.create(GithubReposApi.class); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/NetworkingActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.DefaultItemAnimator; 7 | import android.support.v7.widget.LinearLayoutManager; 8 | import android.support.v7.widget.RecyclerView; 9 | import android.util.Log; 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.ProgressBar; 13 | 14 | import com.example.mahmoud.networking.adapter.RepoAdapter; 15 | import com.example.mahmoud.networking.endpoint.GithubReposApi; 16 | import com.example.mahmoud.networking.model.Repo; 17 | import com.example.mahmoud.samples.R; 18 | 19 | import java.util.List; 20 | 21 | import butterknife.BindView; 22 | import butterknife.ButterKnife; 23 | import butterknife.OnClick; 24 | import io.reactivex.android.schedulers.AndroidSchedulers; 25 | import io.reactivex.schedulers.Schedulers; 26 | import io.reactivex.subjects.PublishSubject; 27 | 28 | /** 29 | * Created by mahmoud on 20/03/18. 30 | */ 31 | 32 | public class NetworkingActivity extends AppCompatActivity { 33 | @BindView(R.id.recycler_view) 34 | RecyclerView rvRepos; 35 | @BindView(R.id.retryBtn) 36 | Button retryBtn; 37 | @BindView(R.id.progressbar) 38 | ProgressBar progressBar; 39 | 40 | PublishSubject retryRequest = PublishSubject.create(); 41 | 42 | @Override 43 | protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_networking); 46 | ButterKnife.bind(this); 47 | 48 | GithubReposApi api = ApiClient.getInstance() 49 | .getGithubReposApi(); 50 | 51 | api.getUserRepos("mrabelwahed") 52 | .doOnError(throwable -> enableRetryBtn()) 53 | .retryWhen(attempt -> attempt.zipWith(retryRequest,(o,o2)->o)) 54 | .subscribeOn(Schedulers.io()) 55 | .observeOn(AndroidSchedulers.mainThread()) 56 | .doOnSubscribe(subscriber->progressBar.setVisibility(View.VISIBLE)) 57 | .doOnDispose(()->progressBar.setVisibility(View.GONE)) 58 | .subscribe(repos -> { 59 | progressBar.setVisibility(View.GONE); 60 | setAdapterData(repos); 61 | }, 62 | throwable -> { 63 | Log.d("error", "error" + throwable.getMessage()); 64 | }); 65 | 66 | } 67 | 68 | private void enableRetryBtn(){ 69 | retryBtn.setVisibility(View.VISIBLE); 70 | } 71 | 72 | void setAdapterData(List repos) { 73 | RepoAdapter adapter = new RepoAdapter(getApplicationContext(), repos); 74 | rvRepos.setLayoutManager(new LinearLayoutManager(this)); 75 | rvRepos.setItemAnimator(new DefaultItemAnimator()); 76 | rvRepos.setAdapter(adapter); 77 | } 78 | 79 | 80 | @OnClick(R.id.retryBtn) 81 | public void retryRequest() { 82 | retryRequest.onNext(System.currentTimeMillis()); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/NetworkingRequestState.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.DefaultItemAnimator; 7 | import android.support.v7.widget.LinearLayoutManager; 8 | import android.support.v7.widget.RecyclerView; 9 | import android.view.View; 10 | import android.widget.Button; 11 | import android.widget.ProgressBar; 12 | import android.widget.Toast; 13 | 14 | import com.example.mahmoud.networking.adapter.RepoAdapter; 15 | import com.example.mahmoud.networking.endpoint.GithubReposApi; 16 | import com.example.mahmoud.networking.model.Repo; 17 | import com.example.mahmoud.networking.state.ApiRequest; 18 | import com.example.mahmoud.networking.state.RequestState; 19 | import com.example.mahmoud.samples.R; 20 | 21 | import java.util.List; 22 | 23 | import butterknife.BindView; 24 | import butterknife.ButterKnife; 25 | import butterknife.OnClick; 26 | import io.reactivex.subjects.BehaviorSubject; 27 | import io.reactivex.subjects.PublishSubject; 28 | 29 | /** 30 | * Created by mahmoud on 21/03/18. 31 | */ 32 | 33 | public class NetworkingRequestState extends AppCompatActivity { 34 | @BindView(R.id.recycler_view) 35 | RecyclerView rvRepos; 36 | @BindView(R.id.retryBtn) 37 | Button retryBtn; 38 | @BindView(R.id.progressbar) 39 | ProgressBar progressBar; 40 | 41 | PublishSubject retryRequest = PublishSubject.create(); 42 | BehaviorSubject state = BehaviorSubject.createDefault(RequestState.IDLE);//receive the recent emission 43 | 44 | @Override 45 | protected void onCreate(@Nullable Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(R.layout.activity_networking); 48 | ButterKnife.bind(this); 49 | 50 | GithubReposApi api = ApiClient.getInstance() 51 | .getGithubReposApi(); 52 | 53 | ApiRequest apiRequest = new ApiRequest(api.getUserRepos("mrabelwahed")); 54 | apiRequest.state.subscribe(requestState->updateLoadingView(requestState)); 55 | apiRequest.errors.subscribe(throwable ->showError(throwable.getMessage())); 56 | apiRequest.repos.subscribe(repos -> setAdapterData(repos)); 57 | apiRequest.execute(); 58 | 59 | } 60 | 61 | private void showError(String msg){ 62 | Toast.makeText(this, "eror"+msg , Toast.LENGTH_SHORT).show(); 63 | } 64 | 65 | private void updateLoadingView(RequestState requestState){ 66 | switch (requestState){ 67 | case IDLE: 68 | break; 69 | case LOADING: 70 | progressBar.setVisibility(View.VISIBLE); 71 | retryBtn.setVisibility(View.GONE); 72 | break; 73 | case COMPLETED: 74 | progressBar.setVisibility(View.GONE); 75 | break; 76 | case ERROR: 77 | progressBar.setVisibility(View.GONE); 78 | retryBtn.setVisibility(View.VISIBLE); 79 | break; 80 | } 81 | } 82 | 83 | private void enableRetryBtn(){ 84 | retryBtn.setVisibility(View.VISIBLE); 85 | } 86 | 87 | void setAdapterData(List repos) { 88 | RepoAdapter adapter = new RepoAdapter(getApplicationContext(), repos); 89 | rvRepos.setLayoutManager(new LinearLayoutManager(this)); 90 | rvRepos.setItemAnimator(new DefaultItemAnimator()); 91 | rvRepos.setAdapter(adapter); 92 | } 93 | 94 | 95 | @OnClick(R.id.retryBtn) 96 | public void retryRequest() { 97 | retryRequest.onNext(System.currentTimeMillis()); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/adapter/RepoAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking.adapter; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import com.example.mahmoud.networking.model.Repo; 11 | import com.example.mahmoud.samples.R; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * Created by mahmoud on 20/03/18. 17 | */ 18 | 19 | public class RepoAdapter extends RecyclerView.Adapter { 20 | private Context context; 21 | private LayoutInflater inflater; 22 | private List repos; 23 | 24 | public RepoAdapter(Context context , Listrepos){ 25 | this.context = context; 26 | this.repos = repos; 27 | this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 28 | } 29 | 30 | @Override 31 | public RepoAdapter.RepoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 32 | View view = inflater.inflate(R.layout.repo_item,parent,false); 33 | return new RepoViewHolder(view); 34 | } 35 | 36 | @Override 37 | public void onBindViewHolder(RepoAdapter.RepoViewHolder holder, int position) { 38 | Repo repo = repos.get(position); 39 | holder.setItemContent(repo); 40 | } 41 | 42 | @Override 43 | public int getItemCount() { 44 | return repos.size(); 45 | } 46 | 47 | class RepoViewHolder extends RecyclerView.ViewHolder{ 48 | 49 | TextView tvRepoName,tvRepoId; 50 | 51 | public RepoViewHolder(View itemView) { 52 | super(itemView); 53 | tvRepoName = (TextView) itemView.findViewById(R.id.repo_name); 54 | tvRepoId = (TextView) itemView.findViewById(R.id.repo_id); 55 | } 56 | 57 | void setItemContent(Repo repo){ 58 | tvRepoName.setText(repo.getName()); 59 | tvRepoId.setText(repo.getId()); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/endpoint/GithubReposApi.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking.endpoint; 2 | 3 | import com.example.mahmoud.networking.model.Repo; 4 | 5 | import java.util.List; 6 | 7 | import io.reactivex.Observable; 8 | import retrofit2.http.GET; 9 | import retrofit2.http.Path; 10 | 11 | /** 12 | * Created by Mahmoud Ramadan on 12/23/17. 13 | */ 14 | 15 | public interface GithubReposApi { 16 | @GET("users/{user}/repos") 17 | Observable> getUserRepos(@Path("user") String username); 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/model/Repo.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * Created by mahmoud on 20/03/18. 7 | */ 8 | 9 | 10 | public class Repo{ 11 | @SerializedName("id") 12 | private String id; 13 | @SerializedName("name") 14 | private String name; 15 | 16 | public String getId() { 17 | return id; 18 | } 19 | 20 | public void setId(String id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/state/ApiRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking.state; 2 | 3 | import com.example.mahmoud.networking.model.Repo; 4 | 5 | import java.util.List; 6 | 7 | import io.reactivex.Observable; 8 | import io.reactivex.android.schedulers.AndroidSchedulers; 9 | import io.reactivex.schedulers.Schedulers; 10 | import io.reactivex.subjects.BehaviorSubject; 11 | 12 | /** 13 | * Created by mahmoud on 21/03/18. 14 | */ 15 | 16 | public class ApiRequest { 17 | public BehaviorSubject trigger = BehaviorSubject.create(); 18 | public BehaviorSubject state = BehaviorSubject.createDefault(RequestState.IDLE); 19 | public BehaviorSubject errors = BehaviorSubject.create(); 20 | public BehaviorSubject> repos = BehaviorSubject.create(); 21 | 22 | public ApiRequest(Observable> observable) { 23 | trigger.doOnNext(val -> state.onNext(RequestState.IDLE)) 24 | .observeOn(Schedulers.io()) 25 | .flatMap(trigger -> observable) 26 | .observeOn(AndroidSchedulers.mainThread()) 27 | .doOnError(throwable -> state.onNext(RequestState.ERROR)) 28 | .onErrorResumeNext(Observable.empty()) 29 | .doOnNext(val -> state.onNext(RequestState.COMPLETED)) 30 | .subscribe(repos); 31 | } 32 | 33 | public void execute() { 34 | trigger.onNext(System.currentTimeMillis()); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/networking/state/RequestState.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.networking.state; 2 | 3 | /** 4 | * Created by mahmoud on 21/03/18. 5 | */ 6 | 7 | public enum RequestState { 8 | IDLE,LOADING,COMPLETED,ERROR; 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/AppInfo.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | /** 6 | * Created by mahmoud on 08/03/18. 7 | */ 8 | 9 | public class AppInfo implements Comparable { 10 | private String name,icon; 11 | private long lastupdate; 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public String getIcon() { 22 | return icon; 23 | } 24 | 25 | public void setIcon(String icon) { 26 | this.icon = icon; 27 | } 28 | 29 | public long getLastupdate() { 30 | return lastupdate; 31 | } 32 | 33 | public void setLastupdate(long lastupdate) { 34 | this.lastupdate = lastupdate; 35 | } 36 | 37 | public AppInfo(String name , String icon , long lastUpdate){ 38 | this.name = name; 39 | this.icon = icon; 40 | this.lastupdate = lastUpdate; 41 | } 42 | 43 | @Override 44 | public int compareTo(@NonNull Object o) { 45 | AppInfo appInfo = (AppInfo) o; 46 | return getName().compareTo(appInfo.getName()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/AppRichInfo.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.ComponentName; 5 | import android.content.Context; 6 | import android.content.pm.PackageInfo; 7 | import android.content.pm.PackageManager; 8 | import android.content.pm.ResolveInfo; 9 | import android.content.res.AssetManager; 10 | import android.content.res.Resources; 11 | import android.graphics.drawable.Drawable; 12 | import android.util.DisplayMetrics; 13 | import android.content.res.Configuration; 14 | 15 | import java.util.Locale; 16 | 17 | /** 18 | * Created by mahmoud on 08/03/18. 19 | */ 20 | 21 | public class AppRichInfo implements Comparable { 22 | 23 | String mName = null; 24 | 25 | private Context mContext; 26 | 27 | private ResolveInfo mResolveInfo; 28 | 29 | private ComponentName mComponentName = null; 30 | 31 | private PackageInfo pi = null; 32 | 33 | private Drawable icon = null; 34 | 35 | public AppRichInfo(Context ctx, ResolveInfo ri) { 36 | mContext = ctx; 37 | mResolveInfo = ri; 38 | 39 | mComponentName = new ComponentName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name); 40 | 41 | try { 42 | pi = ctx.getPackageManager().getPackageInfo(getPackageName(), 0); 43 | } catch (PackageManager.NameNotFoundException e) { 44 | } 45 | } 46 | 47 | public String getName() { 48 | if (mName != null) { 49 | return mName; 50 | } else { 51 | try { 52 | return getNameFromResolveInfo(mResolveInfo); 53 | } catch (PackageManager.NameNotFoundException e) { 54 | e.printStackTrace(); 55 | } 56 | } 57 | return null; 58 | } 59 | 60 | public String getActivityName() { 61 | return mResolveInfo.activityInfo.name; 62 | } 63 | 64 | public String getPackageName() { 65 | return mResolveInfo.activityInfo.packageName; 66 | } 67 | 68 | public ComponentName getComponentName() { 69 | return mComponentName; 70 | } 71 | 72 | public String getComponentInfo() { 73 | if (getComponentName() != null) { 74 | return getComponentName().toString(); 75 | } else { 76 | return ""; 77 | } 78 | } 79 | 80 | public ResolveInfo getResolveInfo() { 81 | return mResolveInfo; 82 | } 83 | 84 | public PackageInfo getPackageInfo() { 85 | return pi; 86 | } 87 | 88 | public String getVersionName() { 89 | PackageInfo pi = getPackageInfo(); 90 | if (pi != null) { 91 | return pi.versionName; 92 | } else { 93 | return ""; 94 | } 95 | } 96 | 97 | public int getVersionCode() { 98 | PackageInfo pi = getPackageInfo(); 99 | if (pi != null) { 100 | return pi.versionCode; 101 | } else { 102 | return 0; 103 | } 104 | } 105 | 106 | public Drawable getIcon() { 107 | if (icon == null) { 108 | icon = getResolveInfo().loadIcon(mContext.getPackageManager()); 109 | /* 110 | Drawable dr = getResolveInfo().loadIcon(mContext.getPackageManager()); 111 | Bitmap bitmap = ((BitmapDrawable) dr).getBitmap(); 112 | icon = new BitmapDrawable(mContext.getResources(), AppHelper.getResizedBitmap(bitmap, 144, 144)); 113 | */ 114 | } 115 | return icon; 116 | } 117 | 118 | @SuppressLint("NewApi") 119 | public long getFirstInstallTime() { 120 | PackageInfo pi = getPackageInfo(); 121 | if (pi != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { 122 | return pi.firstInstallTime; 123 | } else { 124 | return 0; 125 | } 126 | } 127 | 128 | @SuppressLint("NewApi") 129 | public long getLastUpdateTime() { 130 | PackageInfo pi = getPackageInfo(); 131 | if (pi != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { 132 | return pi.lastUpdateTime; 133 | } else { 134 | return 0; 135 | } 136 | } 137 | 138 | @Override 139 | public int compareTo(Object o) { 140 | AppRichInfo f = (AppRichInfo) o; 141 | return getName().compareTo(f.getName()); 142 | } 143 | 144 | @Override 145 | public String toString() { 146 | return getName(); 147 | } 148 | 149 | /** 150 | * Helper method to get an applications name! 151 | */ 152 | 153 | public String getNameFromResolveInfo(ResolveInfo ri) throws PackageManager.NameNotFoundException { 154 | String name = ri.resolvePackageName; 155 | if (ri.activityInfo != null) { 156 | Resources res = mContext.getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo); 157 | Resources engRes = getEnglishRessources(res); 158 | 159 | if (ri.activityInfo.labelRes != 0) { 160 | name = engRes.getString(ri.activityInfo.labelRes); 161 | 162 | if (name == null || name.equals("")) { 163 | name = res.getString(ri.activityInfo.labelRes); 164 | } 165 | 166 | } else { 167 | name = ri.activityInfo.applicationInfo.loadLabel(mContext.getPackageManager()).toString(); 168 | } 169 | } 170 | return name; 171 | } 172 | 173 | public Resources getEnglishRessources(Resources standardResources) { 174 | AssetManager assets = standardResources.getAssets(); 175 | DisplayMetrics metrics = standardResources.getDisplayMetrics(); 176 | Configuration config = new Configuration(standardResources.getConfiguration()); 177 | config.locale = Locale.US; 178 | return new Resources(assets, metrics, config); 179 | } 180 | 181 | } 182 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/AppsAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.BitmapFactory; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.ImageView; 11 | import android.widget.TextView; 12 | 13 | import java.util.List; 14 | 15 | import io.reactivex.Observable; 16 | import io.reactivex.android.schedulers.AndroidSchedulers; 17 | import io.reactivex.schedulers.Schedulers; 18 | 19 | /** 20 | * Created by mahmoud on 08/03/18. 21 | */ 22 | 23 | public class AppsAdapter extends RecyclerView.Adapter{ 24 | 25 | private List mApplications; 26 | 27 | private int mRowLayout; 28 | private Context context; 29 | 30 | public AppsAdapter(List applications, int rowLayout) { 31 | mApplications = applications; 32 | mRowLayout = rowLayout; 33 | } 34 | 35 | public void addApplications(List applications) { 36 | mApplications.clear(); 37 | mApplications.addAll(applications); 38 | notifyDataSetChanged(); 39 | } 40 | 41 | public void addApplication(int position, AppInfo appInfo) { 42 | if (position < 0) { 43 | position = 0; 44 | } 45 | mApplications.add(position, appInfo); 46 | notifyItemInserted(position); 47 | } 48 | 49 | public List getmApplications(){ 50 | return mApplications; 51 | } 52 | 53 | @Override 54 | public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) { 55 | context = viewGroup.getContext(); 56 | View v = LayoutInflater.from(viewGroup.getContext()).inflate(mRowLayout, viewGroup, false); 57 | return new ViewHolder(v); 58 | } 59 | 60 | @Override 61 | public void onBindViewHolder(final ViewHolder viewHolder, int i) { 62 | final AppInfo appInfo = mApplications.get(i); 63 | viewHolder.name.setText(appInfo.getName()); 64 | getBitmap(appInfo.getIcon()) 65 | .subscribeOn(Schedulers.io()) 66 | .observeOn(AndroidSchedulers.mainThread()) 67 | .subscribe(viewHolder.image::setImageBitmap); 68 | } 69 | 70 | private Observable getBitmap(String icon) { 71 | return Observable.create(subscriber -> { 72 | subscriber.onNext(BitmapFactory.decodeFile(icon)); 73 | subscriber.onComplete(); 74 | }); 75 | } 76 | 77 | @Override 78 | public int getItemCount() { 79 | return mApplications == null ? 0 : mApplications.size(); 80 | } 81 | 82 | 83 | 84 | public static class ViewHolder extends RecyclerView.ViewHolder { 85 | 86 | public TextView name; 87 | 88 | public ImageView image; 89 | 90 | public ViewHolder(View itemView) { 91 | super(itemView); 92 | name = (TextView) itemView.findViewById(R.id.name); 93 | image = (ImageView) itemView.findViewById(R.id.image); 94 | } 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/Utils.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.graphics.drawable.BitmapDrawable; 7 | import android.graphics.drawable.Drawable; 8 | 9 | import java.io.FileOutputStream; 10 | import java.io.IOException; 11 | 12 | import io.reactivex.schedulers.Schedulers; 13 | 14 | /** 15 | * Created by mahmoud on 08/03/18. 16 | */ 17 | 18 | public class Utils { 19 | 20 | public static Bitmap drawableToBitmap(Drawable drawable){ 21 | if (drawable instanceof BitmapDrawable) 22 | return ((BitmapDrawable)drawable).getBitmap(); 23 | 24 | Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), 25 | drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 26 | Canvas canvas = new Canvas(bitmap); 27 | drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 28 | drawable.draw(canvas); 29 | return bitmap; 30 | } 31 | 32 | 33 | public static void storeBitmap(Context context, Bitmap bitmap, String filename) { 34 | Schedulers.io().createWorker().schedule(() -> { 35 | blockingStoreBitmap(context, bitmap, filename); 36 | }); 37 | } 38 | 39 | private static void blockingStoreBitmap(Context context, Bitmap bitmap, String filename) { 40 | FileOutputStream fOut = null; 41 | try { 42 | fOut = context.openFileOutput(filename, Context.MODE_PRIVATE); 43 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); 44 | fOut.flush(); 45 | fOut.close(); 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | } finally { 49 | try { 50 | if (fOut != null) { 51 | fOut.close(); 52 | } 53 | } catch (IOException e) { 54 | e.printStackTrace(); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/eighthexample/MapOPeratorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.eighthexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.View; 14 | import android.widget.Toast; 15 | 16 | import com.example.mahmoud.samples.AppInfo; 17 | import com.example.mahmoud.samples.AppRichInfo; 18 | import com.example.mahmoud.samples.AppsAdapter; 19 | import com.example.mahmoud.samples.R; 20 | import com.example.mahmoud.samples.Utils; 21 | 22 | import java.io.File; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import io.reactivex.Observable; 29 | import io.reactivex.android.schedulers.AndroidSchedulers; 30 | import io.reactivex.schedulers.Schedulers; 31 | 32 | /** 33 | * Created by mahmoud on 15/03/18. 34 | */ 35 | 36 | public class MapOPeratorSample extends AppCompatActivity { 37 | @BindView(R.id.recyclerview) 38 | RecyclerView recyclerView; 39 | @BindView(R.id.swipe_refresh_layout) 40 | SwipeRefreshLayout swipeRefreshLayout; 41 | private File mFilesDir; 42 | private AppsAdapter adapter; 43 | private List data = new ArrayList<>(); 44 | 45 | @Override 46 | protected void onCreate(Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_main); 49 | ButterKnife.bind(this); 50 | init(); 51 | getMyApps(); 52 | 53 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 54 | 55 | } 56 | 57 | private void getMyApps() { 58 | getFileDir().subscribeOn(Schedulers.io()) 59 | .observeOn(AndroidSchedulers.mainThread()) 60 | .subscribe(file -> { 61 | mFilesDir = file; 62 | refreshList(); 63 | }); 64 | } 65 | 66 | private void init() { 67 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 68 | 69 | adapter = new AppsAdapter(data, R.layout.item); 70 | recyclerView.setAdapter(adapter); 71 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 72 | swipeRefreshLayout.setProgressViewOffset(false, 0, 73 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 74 | // Progress 75 | swipeRefreshLayout.setEnabled(true); 76 | swipeRefreshLayout.setRefreshing(true); 77 | recyclerView.setVisibility(View.GONE); 78 | } 79 | 80 | private void refreshList() { 81 | Observable.fromIterable(getInstalledApps()) 82 | .map(item ->{ 83 | String lowercase = item.getName().toLowerCase(); 84 | item.setName(lowercase); 85 | return item; 86 | }) 87 | .subscribe(installedApp -> { 88 | Log.d("xx", "xxxxx" + installedApp.getName()); 89 | //set adapter data 90 | recyclerView.setVisibility(View.VISIBLE); 91 | //save data 92 | adapter.addApplication(data.size() - 1, installedApp); 93 | swipeRefreshLayout.setRefreshing(false); 94 | }, 95 | throwable -> { 96 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 97 | swipeRefreshLayout.setRefreshing(false); 98 | }); 99 | } 100 | 101 | 102 | private Observable getFileDir() { 103 | return Observable.create(subscriber -> { 104 | subscriber.onNext(getApplication().getFilesDir()); 105 | subscriber.onComplete(); 106 | }); 107 | } 108 | 109 | private List getInstalledApps() { 110 | 111 | List apps = new ArrayList<>(); 112 | List installedApps = new ArrayList<>(); 113 | 114 | 115 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 116 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 117 | 118 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 119 | for (ResolveInfo info : infos) { 120 | apps.add(new AppRichInfo(getApplication(), info)); 121 | } 122 | 123 | for (AppRichInfo appInfo : apps) { 124 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 125 | String name = appInfo.getName(); 126 | String iconPath = mFilesDir + "/" + name; 127 | Utils.storeBitmap(getApplication(), icon, name); 128 | 129 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 130 | } 131 | return installedApps; 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/firstexample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.firstexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.TypedValue; 12 | import android.view.View; 13 | import android.widget.Toast; 14 | 15 | import com.example.mahmoud.samples.AppInfo; 16 | import com.example.mahmoud.samples.AppRichInfo; 17 | import com.example.mahmoud.samples.AppsAdapter; 18 | import com.example.mahmoud.samples.R; 19 | import com.example.mahmoud.samples.Utils; 20 | 21 | import java.io.File; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import butterknife.BindView; 26 | import butterknife.ButterKnife; 27 | import io.reactivex.Observable; 28 | import io.reactivex.android.schedulers.AndroidSchedulers; 29 | import io.reactivex.schedulers.Schedulers; 30 | 31 | public class MainActivity extends AppCompatActivity { 32 | 33 | @BindView(R.id.recyclerview) 34 | RecyclerView recyclerView; 35 | @BindView(R.id.swipe_refresh_layout) 36 | SwipeRefreshLayout swipeRefreshLayout; 37 | private File mFilesDir; 38 | private AppsAdapter adapter; 39 | 40 | @Override 41 | protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_main); 44 | ButterKnife.bind(this); 45 | init(); 46 | getMyApps(); 47 | 48 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 49 | 50 | } 51 | 52 | private void getMyApps() { 53 | getFileDir().subscribeOn(Schedulers.io()) 54 | .observeOn(AndroidSchedulers.mainThread()) 55 | .subscribe(file -> { 56 | mFilesDir = file; 57 | refreshList(); 58 | }); 59 | } 60 | 61 | private void init() { 62 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 63 | 64 | adapter = new AppsAdapter(new ArrayList<>(), R.layout.item); 65 | recyclerView.setAdapter(adapter); 66 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 67 | swipeRefreshLayout.setProgressViewOffset(false, 0, 68 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 69 | // Progress 70 | swipeRefreshLayout.setEnabled(true); 71 | swipeRefreshLayout.setRefreshing(true); 72 | recyclerView.setVisibility(View.GONE); 73 | } 74 | 75 | private void refreshList() { 76 | getInstalledApps() 77 | .toSortedList() 78 | .subscribe(installedApps -> { 79 | //set adapter data 80 | recyclerView.setVisibility(View.VISIBLE); 81 | //save data 82 | adapter.addApplications(installedApps); 83 | swipeRefreshLayout.setRefreshing(false); 84 | }, 85 | throwable -> { 86 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 87 | swipeRefreshLayout.setRefreshing(false); 88 | }); 89 | } 90 | 91 | 92 | private Observable getFileDir() { 93 | return Observable.create(subscriber -> { 94 | subscriber.onNext(getApplication().getFilesDir()); 95 | subscriber.onComplete(); 96 | }); 97 | } 98 | 99 | private Observable getInstalledApps() { 100 | return Observable.create(subscriber -> { 101 | List apps = new ArrayList<>(); 102 | 103 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 104 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 105 | 106 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 107 | for (ResolveInfo info : infos) { 108 | apps.add(new AppRichInfo(getApplication(), info)); 109 | } 110 | 111 | for (AppRichInfo appInfo : apps) { 112 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 113 | String name = appInfo.getName(); 114 | String iconPath = mFilesDir + "/" + name; 115 | Utils.storeBitmap(getApplication(), icon, name); 116 | 117 | if (subscriber.isDisposed()) { 118 | return; 119 | } 120 | subscriber.onNext(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 121 | } 122 | if (!subscriber.isDisposed()) { 123 | subscriber.onComplete(); 124 | } 125 | }); 126 | } 127 | 128 | 129 | } 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/fivethexample/DistinctOperatorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.fivethexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.View; 14 | import android.widget.Toast; 15 | 16 | import com.example.mahmoud.samples.AppInfo; 17 | import com.example.mahmoud.samples.AppRichInfo; 18 | import com.example.mahmoud.samples.AppsAdapter; 19 | import com.example.mahmoud.samples.R; 20 | import com.example.mahmoud.samples.Utils; 21 | 22 | import java.io.File; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import io.reactivex.Observable; 29 | import io.reactivex.android.schedulers.AndroidSchedulers; 30 | import io.reactivex.schedulers.Schedulers; 31 | 32 | /** 33 | * Created by mahmoud on 11/03/18. 34 | */ 35 | 36 | public class DistinctOperatorSample extends AppCompatActivity { 37 | 38 | @BindView(R.id.recyclerview) 39 | RecyclerView recyclerView; 40 | @BindView(R.id.swipe_refresh_layout) 41 | SwipeRefreshLayout swipeRefreshLayout; 42 | private File mFilesDir; 43 | private AppsAdapter adapter; 44 | private List data = new ArrayList<>(); 45 | @Override 46 | protected void onCreate(Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_main); 49 | ButterKnife.bind(this); 50 | init(); 51 | getMyApps(); 52 | 53 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 54 | 55 | } 56 | 57 | private void getMyApps() { 58 | getFileDir().subscribeOn(Schedulers.io()) 59 | .observeOn(AndroidSchedulers.mainThread()) 60 | .subscribe(file -> { 61 | mFilesDir = file; 62 | refreshList(); 63 | }); 64 | } 65 | 66 | private void init() { 67 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 68 | 69 | adapter = new AppsAdapter(data, R.layout.item); 70 | recyclerView.setAdapter(adapter); 71 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 72 | swipeRefreshLayout.setProgressViewOffset(false, 0, 73 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 74 | // Progress 75 | swipeRefreshLayout.setEnabled(true); 76 | swipeRefreshLayout.setRefreshing(true); 77 | recyclerView.setVisibility(View.GONE); 78 | } 79 | 80 | private void refreshList() { 81 | Observable.fromIterable(getInstalledApps()) 82 | .takeLast(3) //take the last three items 83 | .repeat(3) 84 | .distinct() 85 | .subscribe(installedApp -> { 86 | Log.d("xx","xxxxx"+installedApp.getName()); 87 | //set adapter data 88 | recyclerView.setVisibility(View.VISIBLE); 89 | //save data 90 | adapter.addApplication(data.size()-1,installedApp); 91 | swipeRefreshLayout.setRefreshing(false); 92 | }, 93 | throwable -> { 94 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 95 | swipeRefreshLayout.setRefreshing(false); 96 | }); 97 | } 98 | 99 | 100 | private Observable getFileDir() { 101 | return Observable.create(subscriber -> { 102 | subscriber.onNext(getApplication().getFilesDir()); 103 | subscriber.onComplete(); 104 | }); 105 | } 106 | 107 | private List getInstalledApps() { 108 | 109 | List apps = new ArrayList<>(); 110 | List installedApps = new ArrayList<>(); 111 | 112 | 113 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 114 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 115 | 116 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 117 | for (ResolveInfo info : infos) { 118 | apps.add(new AppRichInfo(getApplication(), info)); 119 | } 120 | 121 | for (AppRichInfo appInfo : apps) { 122 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 123 | String name = appInfo.getName(); 124 | String iconPath = mFilesDir + "/" + name; 125 | Utils.storeBitmap(getApplication(), icon, name); 126 | 127 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 128 | } 129 | return installedApps; 130 | } 131 | 132 | 133 | 134 | } 135 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/fouthexample/TakeOperatorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.fouthexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.View; 14 | import android.widget.Toast; 15 | 16 | import com.example.mahmoud.samples.AppInfo; 17 | import com.example.mahmoud.samples.AppRichInfo; 18 | import com.example.mahmoud.samples.AppsAdapter; 19 | import com.example.mahmoud.samples.R; 20 | import com.example.mahmoud.samples.Utils; 21 | 22 | import java.io.File; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import io.reactivex.Observable; 29 | import io.reactivex.android.schedulers.AndroidSchedulers; 30 | import io.reactivex.schedulers.Schedulers; 31 | 32 | /** 33 | * Created by mahmoud on 11/03/18. 34 | */ 35 | 36 | public class TakeOperatorSample extends AppCompatActivity { 37 | @BindView(R.id.recyclerview) 38 | RecyclerView recyclerView; 39 | @BindView(R.id.swipe_refresh_layout) 40 | SwipeRefreshLayout swipeRefreshLayout; 41 | private File mFilesDir; 42 | private AppsAdapter adapter; 43 | private List data = new ArrayList<>(); 44 | @Override 45 | protected void onCreate(Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(R.layout.activity_main); 48 | ButterKnife.bind(this); 49 | init(); 50 | getMyApps(); 51 | 52 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 53 | 54 | } 55 | 56 | private void getMyApps() { 57 | getFileDir().subscribeOn(Schedulers.io()) 58 | .observeOn(AndroidSchedulers.mainThread()) 59 | .subscribe(file -> { 60 | mFilesDir = file; 61 | refreshList(); 62 | }); 63 | } 64 | 65 | private void init() { 66 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 67 | 68 | adapter = new AppsAdapter(data, R.layout.item); 69 | recyclerView.setAdapter(adapter); 70 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 71 | swipeRefreshLayout.setProgressViewOffset(false, 0, 72 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 73 | // Progress 74 | swipeRefreshLayout.setEnabled(true); 75 | swipeRefreshLayout.setRefreshing(true); 76 | recyclerView.setVisibility(View.GONE); 77 | } 78 | 79 | private void refreshList() { 80 | Observable.fromIterable(getInstalledApps()) 81 | //.take(3) //take the first three items 82 | .takeLast(3) //take the last three items 83 | .subscribe(installedApp -> { 84 | Log.d("xx","xxxxx"+installedApp.getName()); 85 | //set adapter data 86 | recyclerView.setVisibility(View.VISIBLE); 87 | //save data 88 | adapter.addApplication(data.size()-1,installedApp); 89 | swipeRefreshLayout.setRefreshing(false); 90 | }, 91 | throwable -> { 92 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 93 | swipeRefreshLayout.setRefreshing(false); 94 | }); 95 | } 96 | 97 | 98 | private Observable getFileDir() { 99 | return Observable.create(subscriber -> { 100 | subscriber.onNext(getApplication().getFilesDir()); 101 | subscriber.onComplete(); 102 | }); 103 | } 104 | 105 | private List getInstalledApps() { 106 | 107 | List apps = new ArrayList<>(); 108 | List installedApps = new ArrayList<>(); 109 | 110 | 111 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 112 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 113 | 114 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 115 | for (ResolveInfo info : infos) { 116 | apps.add(new AppRichInfo(getApplication(), info)); 117 | } 118 | 119 | for (AppRichInfo appInfo : apps) { 120 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 121 | String name = appInfo.getName(); 122 | String iconPath = mFilesDir + "/" + name; 123 | Utils.storeBitmap(getApplication(), icon, name); 124 | 125 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 126 | } 127 | return installedApps; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/ninthexample/ScanOPeratorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.ninthexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.View; 14 | import android.widget.Toast; 15 | 16 | import com.example.mahmoud.samples.AppInfo; 17 | import com.example.mahmoud.samples.AppRichInfo; 18 | import com.example.mahmoud.samples.AppsAdapter; 19 | import com.example.mahmoud.samples.R; 20 | import com.example.mahmoud.samples.Utils; 21 | 22 | import java.io.File; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import io.reactivex.Observable; 29 | import io.reactivex.android.schedulers.AndroidSchedulers; 30 | import io.reactivex.schedulers.Schedulers; 31 | 32 | /** 33 | * Created by mahmoud on 15/03/18. 34 | */ 35 | 36 | public class ScanOPeratorSample extends AppCompatActivity { 37 | @BindView(R.id.recyclerview) 38 | RecyclerView recyclerView; 39 | @BindView(R.id.swipe_refresh_layout) 40 | SwipeRefreshLayout swipeRefreshLayout; 41 | private File mFilesDir; 42 | private AppsAdapter adapter; 43 | private List data = new ArrayList<>(); 44 | 45 | @Override 46 | protected void onCreate(Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_main); 49 | ButterKnife.bind(this); 50 | init(); 51 | getMyApps(); 52 | 53 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 54 | 55 | } 56 | 57 | private void getMyApps() { 58 | getFileDir().subscribeOn(Schedulers.io()) 59 | .observeOn(AndroidSchedulers.mainThread()) 60 | .subscribe(file -> { 61 | mFilesDir = file; 62 | refreshList(); 63 | }); 64 | } 65 | 66 | private void init() { 67 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 68 | 69 | adapter = new AppsAdapter(data, R.layout.item); 70 | recyclerView.setAdapter(adapter); 71 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 72 | swipeRefreshLayout.setProgressViewOffset(false, 0, 73 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 74 | // Progress 75 | swipeRefreshLayout.setEnabled(true); 76 | swipeRefreshLayout.setRefreshing(true); 77 | recyclerView.setVisibility(View.GONE); 78 | } 79 | 80 | private void refreshList() { 81 | Observable.fromIterable(getInstalledApps()) 82 | .scan((appInfo, appInfo2) -> { 83 | if (appInfo.getName().length()>appInfo2.getName().length()) 84 | return appInfo; 85 | else 86 | return appInfo2; 87 | }) 88 | .distinct() 89 | .subscribe(installedApp -> { 90 | Log.d("xx", "xxxxx" + installedApp.getName()); 91 | //set adapter data 92 | recyclerView.setVisibility(View.VISIBLE); 93 | //save data 94 | adapter.addApplication(data.size() - 1, installedApp); 95 | swipeRefreshLayout.setRefreshing(false); 96 | }, 97 | throwable -> { 98 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 99 | swipeRefreshLayout.setRefreshing(false); 100 | }); 101 | } 102 | 103 | 104 | private Observable getFileDir() { 105 | return Observable.create(subscriber -> { 106 | subscriber.onNext(getApplication().getFilesDir()); 107 | subscriber.onComplete(); 108 | }); 109 | } 110 | 111 | private List getInstalledApps() { 112 | 113 | List apps = new ArrayList<>(); 114 | List installedApps = new ArrayList<>(); 115 | 116 | 117 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 118 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 119 | 120 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 121 | for (ResolveInfo info : infos) { 122 | apps.add(new AppRichInfo(getApplication(), info)); 123 | } 124 | 125 | for (AppRichInfo appInfo : apps) { 126 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 127 | String name = appInfo.getName(); 128 | String iconPath = mFilesDir + "/" + name; 129 | Utils.storeBitmap(getApplication(), icon, name); 130 | 131 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 132 | } 133 | return installedApps; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/secondexample/FormOperatorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.secondexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.TypedValue; 12 | import android.view.View; 13 | import android.widget.Toast; 14 | 15 | import com.example.mahmoud.samples.AppInfo; 16 | import com.example.mahmoud.samples.AppRichInfo; 17 | import com.example.mahmoud.samples.AppsAdapter; 18 | import com.example.mahmoud.samples.R; 19 | import com.example.mahmoud.samples.Utils; 20 | 21 | import java.io.File; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import butterknife.BindView; 26 | import butterknife.ButterKnife; 27 | import io.reactivex.Observable; 28 | import io.reactivex.android.schedulers.AndroidSchedulers; 29 | import io.reactivex.schedulers.Schedulers; 30 | 31 | /** 32 | * Created by mahmoud on 11/03/18. 33 | */ 34 | 35 | public class FormOperatorSample extends AppCompatActivity { 36 | @BindView(R.id.recyclerview) 37 | RecyclerView recyclerView; 38 | @BindView(R.id.swipe_refresh_layout) 39 | SwipeRefreshLayout swipeRefreshLayout; 40 | private File mFilesDir; 41 | private AppsAdapter adapter; 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_main); 47 | ButterKnife.bind(this); 48 | init(); 49 | getMyApps(); 50 | 51 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 52 | 53 | } 54 | 55 | private void getMyApps() { 56 | getFileDir().subscribeOn(Schedulers.io()) 57 | .observeOn(AndroidSchedulers.mainThread()) 58 | .subscribe(file -> { 59 | mFilesDir = file; 60 | refreshList(); 61 | }); 62 | } 63 | 64 | private void init() { 65 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 66 | 67 | adapter = new AppsAdapter(new ArrayList<>(), R.layout.item); 68 | recyclerView.setAdapter(adapter); 69 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 70 | swipeRefreshLayout.setProgressViewOffset(false, 0, 71 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 72 | // Progress 73 | swipeRefreshLayout.setEnabled(true); 74 | swipeRefreshLayout.setRefreshing(true); 75 | recyclerView.setVisibility(View.GONE); 76 | } 77 | 78 | private void refreshList() { 79 | Observable.fromIterable(getInstalledApps()) 80 | .toSortedList() 81 | .subscribe(installedApps -> { 82 | //set adapter data 83 | recyclerView.setVisibility(View.VISIBLE); 84 | //save data 85 | adapter.addApplications(installedApps); 86 | swipeRefreshLayout.setRefreshing(false); 87 | }, 88 | throwable -> { 89 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 90 | swipeRefreshLayout.setRefreshing(false); 91 | }); 92 | } 93 | 94 | 95 | private Observable getFileDir() { 96 | return Observable.create(subscriber -> { 97 | subscriber.onNext(getApplication().getFilesDir()); 98 | subscriber.onComplete(); 99 | }); 100 | } 101 | 102 | private List getInstalledApps() { 103 | 104 | List apps = new ArrayList<>(); 105 | List installedApps = new ArrayList<>(); 106 | 107 | 108 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 109 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 110 | 111 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 112 | for (ResolveInfo info : infos) { 113 | apps.add(new AppRichInfo(getApplication(), info)); 114 | } 115 | 116 | for (AppRichInfo appInfo : apps) { 117 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 118 | String name = appInfo.getName(); 119 | String iconPath = mFilesDir + "/" + name; 120 | Utils.storeBitmap(getApplication(), icon, name); 121 | 122 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 123 | } 124 | return installedApps; 125 | } 126 | } 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/seventhexample/SkipOperatorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.seventhexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.View; 14 | import android.widget.Toast; 15 | 16 | import com.example.mahmoud.samples.AppInfo; 17 | import com.example.mahmoud.samples.AppRichInfo; 18 | import com.example.mahmoud.samples.AppsAdapter; 19 | import com.example.mahmoud.samples.R; 20 | import com.example.mahmoud.samples.Utils; 21 | 22 | import java.io.File; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import io.reactivex.Observable; 29 | import io.reactivex.android.schedulers.AndroidSchedulers; 30 | import io.reactivex.schedulers.Schedulers; 31 | 32 | /** 33 | * Created by mahmoud on 11/03/18. 34 | */ 35 | 36 | /** 37 | * Created by mahmoud on 11/03/18. 38 | */ 39 | 40 | public class SkipOperatorSample extends AppCompatActivity { 41 | 42 | @BindView(R.id.recyclerview) 43 | RecyclerView recyclerView; 44 | @BindView(R.id.swipe_refresh_layout) 45 | SwipeRefreshLayout swipeRefreshLayout; 46 | private File mFilesDir; 47 | private AppsAdapter adapter; 48 | private List data = new ArrayList<>(); 49 | 50 | @Override 51 | protected void onCreate(Bundle savedInstanceState) { 52 | super.onCreate(savedInstanceState); 53 | setContentView(R.layout.activity_main); 54 | ButterKnife.bind(this); 55 | init(); 56 | getMyApps(); 57 | 58 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 59 | 60 | } 61 | 62 | private void getMyApps() { 63 | getFileDir().subscribeOn(Schedulers.io()) 64 | .observeOn(AndroidSchedulers.mainThread()) 65 | .subscribe(file -> { 66 | mFilesDir = file; 67 | refreshList(); 68 | }); 69 | } 70 | 71 | private void init() { 72 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 73 | 74 | adapter = new AppsAdapter(data, R.layout.item); 75 | recyclerView.setAdapter(adapter); 76 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 77 | swipeRefreshLayout.setProgressViewOffset(false, 0, 78 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 79 | // Progress 80 | swipeRefreshLayout.setEnabled(true); 81 | swipeRefreshLayout.setRefreshing(true); 82 | recyclerView.setVisibility(View.GONE); 83 | } 84 | 85 | private void refreshList() { 86 | Observable.fromIterable(getInstalledApps()) 87 | // .skip(2) 88 | //.skipLast(2) 89 | .elementAtOrError(2)//get the item at certain position 90 | .subscribe(installedApp -> { 91 | Log.d("xx", "xxxxx" + installedApp.getName()); 92 | //set adapter data 93 | recyclerView.setVisibility(View.VISIBLE); 94 | //save data 95 | adapter.addApplication(data.size() - 1, installedApp); 96 | swipeRefreshLayout.setRefreshing(false); 97 | }, 98 | throwable -> { 99 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 100 | swipeRefreshLayout.setRefreshing(false); 101 | }); 102 | } 103 | 104 | 105 | private Observable getFileDir() { 106 | return Observable.create(subscriber -> { 107 | subscriber.onNext(getApplication().getFilesDir()); 108 | subscriber.onComplete(); 109 | }); 110 | } 111 | 112 | private List getInstalledApps() { 113 | 114 | List apps = new ArrayList<>(); 115 | List installedApps = new ArrayList<>(); 116 | 117 | 118 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 119 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 120 | 121 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 122 | for (ResolveInfo info : infos) { 123 | apps.add(new AppRichInfo(getApplication(), info)); 124 | } 125 | 126 | for (AppRichInfo appInfo : apps) { 127 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 128 | String name = appInfo.getName(); 129 | String iconPath = mFilesDir + "/" + name; 130 | Utils.storeBitmap(getApplication(), icon, name); 131 | 132 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 133 | } 134 | return installedApps; 135 | } 136 | 137 | 138 | } 139 | 140 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/sixthexample/FirstOperatorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.fivethexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.View; 14 | import android.widget.Toast; 15 | 16 | import com.example.mahmoud.samples.AppInfo; 17 | import com.example.mahmoud.samples.AppRichInfo; 18 | import com.example.mahmoud.samples.AppsAdapter; 19 | import com.example.mahmoud.samples.R; 20 | import com.example.mahmoud.samples.Utils; 21 | 22 | import java.io.File; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import io.reactivex.Observable; 29 | import io.reactivex.android.schedulers.AndroidSchedulers; 30 | import io.reactivex.schedulers.Schedulers; 31 | 32 | /** 33 | * Created by mahmoud on 11/03/18. 34 | */ 35 | 36 | public class FirstOperatorSample extends AppCompatActivity { 37 | 38 | @BindView(R.id.recyclerview) 39 | RecyclerView recyclerView; 40 | @BindView(R.id.swipe_refresh_layout) 41 | SwipeRefreshLayout swipeRefreshLayout; 42 | private File mFilesDir; 43 | private AppsAdapter adapter; 44 | private List data = new ArrayList<>(); 45 | @Override 46 | protected void onCreate(Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_main); 49 | ButterKnife.bind(this); 50 | init(); 51 | getMyApps(); 52 | 53 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 54 | 55 | } 56 | 57 | private void getMyApps() { 58 | getFileDir().subscribeOn(Schedulers.io()) 59 | .observeOn(AndroidSchedulers.mainThread()) 60 | .subscribe(file -> { 61 | mFilesDir = file; 62 | refreshList(); 63 | }); 64 | } 65 | 66 | private void init() { 67 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 68 | 69 | adapter = new AppsAdapter(data, R.layout.item); 70 | recyclerView.setAdapter(adapter); 71 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 72 | swipeRefreshLayout.setProgressViewOffset(false, 0, 73 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 74 | // Progress 75 | swipeRefreshLayout.setEnabled(true); 76 | swipeRefreshLayout.setRefreshing(true); 77 | recyclerView.setVisibility(View.GONE); 78 | } 79 | 80 | private void refreshList() { 81 | Observable.fromIterable(getInstalledApps()) 82 | // .firstElement() 83 | .lastElement() 84 | .subscribe(installedApp -> { 85 | Log.d("xx","xxxxx"+installedApp.getName()); 86 | //set adapter data 87 | recyclerView.setVisibility(View.VISIBLE); 88 | //save data 89 | adapter.addApplication(data.size()-1,installedApp); 90 | swipeRefreshLayout.setRefreshing(false); 91 | }, 92 | throwable -> { 93 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 94 | swipeRefreshLayout.setRefreshing(false); 95 | }); 96 | } 97 | 98 | 99 | private Observable getFileDir() { 100 | return Observable.create(subscriber -> { 101 | subscriber.onNext(getApplication().getFilesDir()); 102 | subscriber.onComplete(); 103 | }); 104 | } 105 | 106 | private List getInstalledApps() { 107 | 108 | List apps = new ArrayList<>(); 109 | List installedApps = new ArrayList<>(); 110 | 111 | 112 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 113 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 114 | 115 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 116 | for (ResolveInfo info : infos) { 117 | apps.add(new AppRichInfo(getApplication(), info)); 118 | } 119 | 120 | for (AppRichInfo appInfo : apps) { 121 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 122 | String name = appInfo.getName(); 123 | String iconPath = mFilesDir + "/" + name; 124 | Utils.storeBitmap(getApplication(), icon, name); 125 | 126 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 127 | } 128 | return installedApps; 129 | } 130 | 131 | 132 | 133 | } 134 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/samples/thirdexample/FilterOPeratorSample.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.samples.thirdexample; 2 | 3 | import android.content.Intent; 4 | import android.content.pm.ResolveInfo; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.support.v4.widget.SwipeRefreshLayout; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.util.Log; 12 | import android.util.TypedValue; 13 | import android.view.View; 14 | import android.widget.Toast; 15 | 16 | import com.example.mahmoud.samples.AppInfo; 17 | import com.example.mahmoud.samples.AppRichInfo; 18 | import com.example.mahmoud.samples.AppsAdapter; 19 | import com.example.mahmoud.samples.R; 20 | import com.example.mahmoud.samples.Utils; 21 | 22 | import java.io.File; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import butterknife.BindView; 27 | import butterknife.ButterKnife; 28 | import io.reactivex.Observable; 29 | import io.reactivex.android.schedulers.AndroidSchedulers; 30 | import io.reactivex.schedulers.Schedulers; 31 | 32 | /** 33 | * Created by mahmoud on 11/03/18. 34 | */ 35 | 36 | public class FilterOPeratorSample extends AppCompatActivity { 37 | @BindView(R.id.recyclerview) 38 | RecyclerView recyclerView; 39 | @BindView(R.id.swipe_refresh_layout) 40 | SwipeRefreshLayout swipeRefreshLayout; 41 | private File mFilesDir; 42 | private AppsAdapter adapter; 43 | private List data = new ArrayList<>(); 44 | @Override 45 | protected void onCreate(Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(R.layout.activity_main); 48 | ButterKnife.bind(this); 49 | init(); 50 | getMyApps(); 51 | 52 | swipeRefreshLayout.setOnRefreshListener(() -> getMyApps()); 53 | 54 | } 55 | 56 | private void getMyApps() { 57 | getFileDir().subscribeOn(Schedulers.io()) 58 | .observeOn(AndroidSchedulers.mainThread()) 59 | .subscribe(file -> { 60 | mFilesDir = file; 61 | refreshList(); 62 | }); 63 | } 64 | 65 | private void init() { 66 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 67 | 68 | adapter = new AppsAdapter(data, R.layout.item); 69 | recyclerView.setAdapter(adapter); 70 | swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary)); 71 | swipeRefreshLayout.setProgressViewOffset(false, 0, 72 | (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics())); 73 | // Progress 74 | swipeRefreshLayout.setEnabled(true); 75 | swipeRefreshLayout.setRefreshing(true); 76 | recyclerView.setVisibility(View.GONE); 77 | } 78 | 79 | private void refreshList() { 80 | Observable.fromIterable(getInstalledApps()) 81 | .filter(appInfo -> appInfo.getName().startsWith("C")) 82 | .subscribe(installedApp -> { 83 | Log.d("xx","xxxxx"+installedApp.getName()); 84 | //set adapter data 85 | recyclerView.setVisibility(View.VISIBLE); 86 | //save data 87 | adapter.addApplication(data.size()-1,installedApp); 88 | swipeRefreshLayout.setRefreshing(false); 89 | }, 90 | throwable -> { 91 | Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 92 | swipeRefreshLayout.setRefreshing(false); 93 | }); 94 | } 95 | 96 | 97 | private Observable getFileDir() { 98 | return Observable.create(subscriber -> { 99 | subscriber.onNext(getApplication().getFilesDir()); 100 | subscriber.onComplete(); 101 | }); 102 | } 103 | 104 | private List getInstalledApps() { 105 | 106 | List apps = new ArrayList<>(); 107 | List installedApps = new ArrayList<>(); 108 | 109 | 110 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 111 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 112 | 113 | List infos = getApplication().getPackageManager().queryIntentActivities(mainIntent, 0); 114 | for (ResolveInfo info : infos) { 115 | apps.add(new AppRichInfo(getApplication(), info)); 116 | } 117 | 118 | for (AppRichInfo appInfo : apps) { 119 | Bitmap icon = Utils.drawableToBitmap(appInfo.getIcon()); 120 | String name = appInfo.getName(); 121 | String iconPath = mFilesDir + "/" + name; 122 | Utils.storeBitmap(getApplication(), icon, name); 123 | 124 | installedApps.add(new AppInfo(name, iconPath, appInfo.getLastUpdateTime())); 125 | } 126 | return installedApps; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/timing/SplashScreen.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.timing; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.Log; 8 | 9 | import com.example.mahmoud.samples.R; 10 | 11 | import java.util.concurrent.TimeUnit; 12 | 13 | import io.reactivex.Flowable; 14 | import io.reactivex.disposables.CompositeDisposable; 15 | import io.reactivex.disposables.Disposable; 16 | 17 | /** 18 | * Created by mahmoud on 21/03/18. 19 | */ 20 | 21 | public class SplashScreen extends AppCompatActivity { 22 | CompositeDisposable compositeDisposable; 23 | @Override 24 | protected void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_splash); 27 | 28 | compositeDisposable = new CompositeDisposable(); 29 | Disposable disposable = 30 | Flowable.timer(2, TimeUnit.SECONDS) 31 | .subscribe(val -> { 32 | startActivity(new Intent(this,TimingActivity.class)); 33 | finish(); 34 | }, 35 | throwable -> { 36 | Log.d("error", "error" + throwable.getMessage()); 37 | }); 38 | 39 | compositeDisposable.add(disposable); 40 | } 41 | 42 | @Override 43 | protected void onDestroy() { 44 | super.onDestroy(); 45 | compositeDisposable.clear(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/mahmoud/timing/TimingActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.mahmoud.timing; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | 8 | import com.example.mahmoud.samples.R; 9 | 10 | import java.text.SimpleDateFormat; 11 | import java.util.Date; 12 | import java.util.Locale; 13 | import java.util.concurrent.TimeUnit; 14 | 15 | import butterknife.ButterKnife; 16 | import butterknife.OnClick; 17 | import io.reactivex.Flowable; 18 | 19 | /** 20 | * Created by mahmoud on 20/03/18. 21 | */ 22 | 23 | public class TimingActivity extends AppCompatActivity { 24 | 25 | @Override 26 | protected void onCreate(@Nullable Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_timing); 29 | ButterKnife.bind(this); 30 | } 31 | 32 | @OnClick(R.id.timing1) 33 | public void runSingleTaskAfter2Sec() { 34 | Log.d("btn_clicked","clicked:"+getCurrentTimestamp()); 35 | Flowable.timer(2, TimeUnit.SECONDS) 36 | .subscribe(val -> { 37 | Log.d("next", "next:" + getCurrentTimestamp()); 38 | }, 39 | throwable -> { 40 | Log.d("error", "error" + throwable.getMessage()); 41 | }, 42 | ()->{ 43 | Log.d("complete",getCurrentTimestamp()); 44 | }); 45 | } 46 | 47 | @OnClick(R.id.timing2) 48 | public void doRepeatedTaskEvery1sec() { 49 | Log.d("btn_clicked","clicked:"+getCurrentTimestamp()); 50 | Flowable.interval(1, TimeUnit.SECONDS) 51 | .subscribe(val -> { 52 | Log.d("next", "next:" + getCurrentTimestamp()); 53 | }, 54 | throwable -> { 55 | Log.d("error", "error" + throwable.getMessage()); 56 | }, 57 | ()->{ 58 | Log.d("complete",getCurrentTimestamp()); 59 | }); 60 | 61 | } 62 | 63 | 64 | 65 | private String getCurrentTimestamp() { 66 | return new SimpleDateFormat("k:m:s:S a", Locale.getDefault()).format(new Date()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lastknown_location.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 14 | 15 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_networking.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 |