├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── tonytang │ │ └── demo │ │ ├── constants │ │ └── Constants.java │ │ ├── entity │ │ └── Movie.java │ │ ├── model │ │ └── MoviesWrapper.java │ │ ├── retrofit │ │ └── service │ │ │ ├── CacheConfigInterceptor.java │ │ │ ├── MovieService.java │ │ │ └── RestClient.java │ │ ├── ui │ │ ├── activity │ │ │ ├── AndroidApplication.java │ │ │ └── MainActivity.java │ │ ├── adapter │ │ │ └── MovieAdapter.java │ │ ├── decoration │ │ │ └── DividerItemDecoration.java │ │ └── fragment │ │ │ └── MovieSearchFragment.java │ │ └── util │ │ └── AppUtils.java │ └── res │ ├── layout │ ├── activity_main.xml │ ├── fragment_movie.xml │ ├── item_movie.xml │ └── view_empty.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── versioning.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea 3 | .gradle 4 | local.properties 5 | .DS_Store 6 | build 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | An android demo for Rxjava2, retrofit2 and OkHttp3 -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'me.tatarka.retrolambda' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "25.0.2" 7 | 8 | defaultConfig { 9 | applicationId APPPLICATION_ID 10 | minSdkVersion 15 11 | targetSdkVersion 25 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | 16 | compileOptions { 17 | sourceCompatibility JavaVersion.VERSION_1_8 18 | targetCompatibility JavaVersion.VERSION_1_8 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | 28 | } 29 | 30 | dependencies { 31 | compile parent.ext.libraries.retrofit 32 | compile parent.ext.libraries.support_v13 33 | compile parent.ext.libraries.appcompat 34 | compile parent.ext.libraries.okhttp_urlconnection 35 | compile parent.ext.libraries.okhttp_logging_interceptor 36 | compile parent.ext.libraries.okhttp 37 | compile parent.ext.libraries.support_annotations 38 | compile parent.ext.libraries.recyclerview 39 | compile parent.ext.libraries.cardview 40 | compile parent.ext.libraries.converter_gson 41 | compile parent.ext.libraries.adapter_rxjava2 42 | 43 | compile 'com.github.JakeWharton.RxBinding:rxbinding:ec6db9d' 44 | compile 'com.google.code.gson:gson:2.8.0' 45 | compile 'io.reactivex.rxjava2:rxandroid:2.0.1' 46 | compile 'com.github.bumptech.glide:glide:3.7.0' 47 | compile 'com.jakewharton:butterknife:7.0.1' 48 | provided 'javax.annotation:jsr250-api:1.0' 49 | compile('com.github.bumptech.glide:okhttp3-integration:1.4.0') { 50 | exclude group: 'glide-parent' 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/tonythompson/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/constants/Constants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); 3 | * you may not use this file except in compliance with the License. 4 | * You may obtain a copy of the License at 5 | *

6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | *

8 | * Unless required by applicable law or agreed to in writing, software 9 | * distributed under the License is distributed on an "AS IS" BASIS, 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | package com.tonytang.demo.constants; 15 | 16 | 17 | public class Constants { 18 | 19 | public static final String API_KEY = "87a901020f496977f9d6d508c5d186ec"; 20 | public static final String MOVIE_DB_HOST = "http://api.themoviedb.org/3/"; 21 | public static String BASIC_STATIC_URL = "http://image.tmdb.org/t/p/w780/"; 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/entity/Movie.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); 3 | * you may not use this file except in compliance with the License. 4 | * You may obtain a copy of the License at 5 | *

6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | *

8 | * Unless required by applicable law or agreed to in writing, software 9 | * distributed under the License is distributed on an "AS IS" BASIS, 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the License for the specific language governing permissions and 12 | * limitations under the License. 13 | */ 14 | package com.tonytang.demo.entity; 15 | 16 | import java.io.Serializable; 17 | 18 | public class Movie implements Serializable { 19 | 20 | private String poster_path; 21 | private boolean adult; 22 | private String overview; 23 | private String release_date; 24 | private Number[] genre_ids; 25 | private Number id; 26 | private String original_title; 27 | private String original_language; 28 | private String title; 29 | private String backdrop_path; 30 | private Number popularity; 31 | private Number vote_count; 32 | private boolean video; 33 | private Number vote_average; 34 | 35 | 36 | public String getPoster_path() { 37 | return poster_path; 38 | } 39 | 40 | public void setPoster_path(String poster_path) { 41 | this.poster_path = poster_path; 42 | } 43 | 44 | public boolean isAdult() { 45 | return adult; 46 | } 47 | 48 | public void setAdult(boolean adult) { 49 | this.adult = adult; 50 | } 51 | 52 | public String getOverview() { 53 | return overview; 54 | } 55 | 56 | public void setOverview(String overview) { 57 | this.overview = overview; 58 | } 59 | 60 | public String getRelease_date() { 61 | return release_date; 62 | } 63 | 64 | public void setRelease_date(String release_date) { 65 | this.release_date = release_date; 66 | } 67 | 68 | public Number[] getGenre_ids() { 69 | return genre_ids; 70 | } 71 | 72 | public void setGenre_ids(Number[] genre_ids) { 73 | this.genre_ids = genre_ids; 74 | } 75 | 76 | public Number getId() { 77 | return id; 78 | } 79 | 80 | public void setId(Number id) { 81 | this.id = id; 82 | } 83 | 84 | public String getOriginal_title() { 85 | return original_title; 86 | } 87 | 88 | public void setOriginal_title(String original_title) { 89 | this.original_title = original_title; 90 | } 91 | 92 | public String getOriginal_language() { 93 | return original_language; 94 | } 95 | 96 | public void setOriginal_language(String original_language) { 97 | this.original_language = original_language; 98 | } 99 | 100 | public String getTitle() { 101 | return title; 102 | } 103 | 104 | public void setTitle(String title) { 105 | this.title = title; 106 | } 107 | 108 | public String getBackdrop_path() { 109 | return backdrop_path; 110 | } 111 | 112 | public void setBackdrop_path(String backdrop_path) { 113 | this.backdrop_path = backdrop_path; 114 | } 115 | 116 | public Number getPopularity() { 117 | return popularity; 118 | } 119 | 120 | public void setPopularity(Number popularity) { 121 | this.popularity = popularity; 122 | } 123 | 124 | public Number getVote_count() { 125 | return vote_count; 126 | } 127 | 128 | public void setVote_count(Number vote_count) { 129 | this.vote_count = vote_count; 130 | } 131 | 132 | public boolean isVideo() { 133 | return video; 134 | } 135 | 136 | public void setVideo(boolean video) { 137 | this.video = video; 138 | } 139 | 140 | public Number getVote_average() { 141 | return vote_average; 142 | } 143 | 144 | public void setVote_average(Number vote_average) { 145 | this.vote_average = vote_average; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/model/MoviesWrapper.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.model; 2 | 3 | 4 | import com.tonytang.demo.entity.Movie; 5 | 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | public class MoviesWrapper implements Serializable { 10 | 11 | private Number page; 12 | 13 | private List results; 14 | 15 | private Number total_pages; 16 | private Number total_results; 17 | 18 | public MoviesWrapper(List results) { 19 | 20 | this.results = results; 21 | } 22 | 23 | public Number getPage() { 24 | 25 | return this.page; 26 | } 27 | 28 | public void setPage(Number page) { 29 | 30 | this.page = page; 31 | } 32 | 33 | public List getResults() { 34 | 35 | return results; 36 | } 37 | 38 | public Number getTotal_pages() { 39 | 40 | return this.total_pages; 41 | } 42 | 43 | public void setTotal_pages(Number total_pages) { 44 | 45 | this.total_pages = total_pages; 46 | } 47 | 48 | public Number getTotal_results() { 49 | 50 | return this.total_results; 51 | } 52 | 53 | public void setTotal_results(Number total_results) { 54 | 55 | this.total_results = total_results; 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/retrofit/service/CacheConfigInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.retrofit.service; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.tonytang.demo.util.AppUtils; 6 | 7 | import java.io.IOException; 8 | 9 | import okhttp3.Interceptor; 10 | import okhttp3.Request; 11 | import okhttp3.Response; 12 | 13 | 14 | /** 15 | * This is an interceptor to config the cache of the http response. 16 | * Based on network status, the cache valid interval will be configured differently. 17 | * When the network is connected, its valid interval is only 10 seconds as we could always retrieve 18 | * the data from server. 19 | * When the network is disconnected, it will accept the cache response in the past month of such request. 20 | *

21 | *

22 | * You could test it out by searching a keyword. Then cut off all network including mobile network 23 | * and search the same keyword again. You will still get the result. 24 | *

25 | * In this case, we do not have to manage cache by ourselves. 26 | */ 27 | public final class CacheConfigInterceptor implements Interceptor { 28 | 29 | public static final long CACHE_DURATION_WITH_NETWORK_IN_SECONDS = 10;//expired in 10 seconds. 30 | public static final long CACHE_DURATION_WITHOUT_NETWORK_IN_SECONDS = 7 * 24 * 60 * 60;//expired in once week. 31 | 32 | @Override 33 | public Response intercept(Chain chain) throws IOException { 34 | Request originalRequest = chain.request(); 35 | Request compressedRequest = originalRequest.newBuilder() 36 | .header("Cache-Control", getCacheConfig()).build(); 37 | return chain.proceed(compressedRequest); 38 | } 39 | 40 | @NonNull 41 | private String getCacheConfig() { 42 | return AppUtils.isConnected() ? getCacheConfigOnNetworkConnected() : getCacheConfigOnNetworkDisconnected(); 43 | } 44 | 45 | 46 | @NonNull 47 | private String getCacheConfigOnNetworkDisconnected() { 48 | //hard code here as it is fixed settings. 49 | return "public, only-if-cached, max-stale=" + CACHE_DURATION_WITHOUT_NETWORK_IN_SECONDS; 50 | } 51 | 52 | @NonNull 53 | private String getCacheConfigOnNetworkConnected() { 54 | //hard code here as it is fixed settings. 55 | return "public, max-age=" + CACHE_DURATION_WITH_NETWORK_IN_SECONDS + ", max-stale=" + CACHE_DURATION_WITH_NETWORK_IN_SECONDS; 56 | } 57 | 58 | 59 | } -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/retrofit/service/MovieService.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.retrofit.service; 2 | 3 | import com.tonytang.demo.model.MoviesWrapper; 4 | 5 | import io.reactivex.Observable; 6 | import retrofit2.http.GET; 7 | import retrofit2.http.Query; 8 | 9 | 10 | public interface MovieService { 11 | 12 | @GET("search/movie") 13 | Observable searchRx(@Query("api_key") String apiKey, @Query("query") String query); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/retrofit/service/RestClient.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.retrofit.service; 2 | 3 | import com.tonytang.demo.BuildConfig; 4 | import com.tonytang.demo.constants.Constants; 5 | import com.tonytang.demo.ui.activity.AndroidApplication; 6 | 7 | import java.io.File; 8 | 9 | import okhttp3.Cache; 10 | import okhttp3.OkHttpClient; 11 | import okhttp3.logging.HttpLoggingInterceptor; 12 | import retrofit2.Retrofit; 13 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; 14 | import retrofit2.converter.gson.GsonConverterFactory; 15 | 16 | public class RestClient { 17 | private static final String BASE_URL = Constants.MOVIE_DB_HOST; 18 | private static final String CACHE_DIRECTORY_RETROFIT = "cache_directory"; 19 | private static final long CACHE_SIZE_RETROFIT = 1000 * 1024; 20 | private final MovieService movieService; 21 | 22 | public RestClient() { 23 | File httpCacheDirectory = new File(AndroidApplication.getInstance().getCacheDir(), CACHE_DIRECTORY_RETROFIT); 24 | Cache httpResponseCache = new Cache(httpCacheDirectory, CACHE_SIZE_RETROFIT); 25 | 26 | HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 27 | logging.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BASIC : HttpLoggingInterceptor.Level.NONE); 28 | OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 29 | httpClient.addInterceptor(logging); 30 | httpClient.addInterceptor(new CacheConfigInterceptor()); 31 | httpClient.cache(httpResponseCache); 32 | Retrofit retrofit = new Retrofit.Builder() 33 | .baseUrl(BASE_URL).addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 34 | .addConverterFactory(GsonConverterFactory.create()) 35 | .client(httpClient.build()) 36 | .build(); 37 | 38 | 39 | movieService = retrofit.create(MovieService.class); 40 | 41 | } 42 | 43 | public MovieService getMovieService() { 44 | return movieService; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/ui/activity/AndroidApplication.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.ui.activity; 2 | 3 | import android.app.Application; 4 | 5 | import com.tonytang.demo.retrofit.service.RestClient; 6 | 7 | /** 8 | * Created by tonythompson on 3/8/16. 9 | */ 10 | public class AndroidApplication extends Application { 11 | 12 | 13 | private static AndroidApplication application; 14 | private RestClient restClient; 15 | 16 | public static AndroidApplication getInstance() { 17 | return application; 18 | } 19 | 20 | @Override 21 | public void onCreate() { 22 | super.onCreate(); 23 | application = this; 24 | restClient = new RestClient(); 25 | } 26 | 27 | public RestClient getRestClient() { 28 | return restClient; 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/ui/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.ui.activity; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | 7 | import com.tonytang.demo.R; 8 | import com.tonytang.demo.ui.fragment.MovieSearchFragment; 9 | 10 | public class MainActivity extends AppCompatActivity { 11 | 12 | @Override 13 | protected void onCreate(@Nullable Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_main); 16 | getFragmentManager().beginTransaction().replace(R.id.fragment_holder, 17 | MovieSearchFragment.newInstance()).commit(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/ui/adapter/MovieAdapter.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.ui.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.ImageView; 9 | import android.widget.TextView; 10 | 11 | import com.bumptech.glide.Glide; 12 | import com.tonytang.demo.R; 13 | import com.tonytang.demo.constants.Constants; 14 | import com.tonytang.demo.entity.Movie; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | import butterknife.Bind; 20 | import butterknife.ButterKnife; 21 | 22 | public class MovieAdapter extends RecyclerView.Adapter { 23 | 24 | private final Context context; 25 | private List dataList = new ArrayList<>(); 26 | 27 | public MovieAdapter(Context context) { 28 | 29 | this.context = context; 30 | } 31 | 32 | public void updateDataSet(List dataList) { 33 | this.dataList = dataList; 34 | notifyDataSetChanged(); 35 | } 36 | 37 | @Override 38 | public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 39 | return onCreateViewHolder(parent); 40 | } 41 | 42 | 43 | public MovieViewHolder onCreateViewHolder(ViewGroup parent) { 44 | return new MovieViewHolder(LayoutInflater.from(parent.getContext()) 45 | .inflate(R.layout.item_movie, parent, false)); 46 | } 47 | 48 | @Override 49 | public void onBindViewHolder(MovieViewHolder holder, int position) { 50 | Movie currentItem = dataList.get(position); 51 | holder.tv_title.setText(currentItem.getTitle()); 52 | Glide.with(context).load(Constants.BASIC_STATIC_URL + currentItem.getPoster_path()).into(holder.iv_cover); 53 | holder.tv_title.setText(currentItem.getTitle()); 54 | 55 | } 56 | 57 | 58 | @Override 59 | public int getItemCount() { 60 | return dataList.size(); 61 | } 62 | 63 | public static class MovieViewHolder extends RecyclerView.ViewHolder { 64 | 65 | @Bind(R.id.iv_cover) 66 | public ImageView iv_cover; 67 | @Bind(R.id.tv_title) 68 | public TextView tv_title; 69 | 70 | public MovieViewHolder(View itemView) { 71 | super(itemView); 72 | ButterKnife.bind(this, itemView); 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/ui/decoration/DividerItemDecoration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.tonytang.demo.ui.decoration; 18 | 19 | import android.content.Context; 20 | import android.content.res.TypedArray; 21 | import android.graphics.Canvas; 22 | import android.graphics.drawable.Drawable; 23 | import android.support.v4.view.ViewCompat; 24 | import android.support.v7.widget.LinearLayoutManager; 25 | import android.support.v7.widget.RecyclerView; 26 | import android.view.View; 27 | 28 | public class DividerItemDecoration extends RecyclerView.ItemDecoration { 29 | 30 | public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 31 | public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; 32 | private static final int[] ATTRS = new int[]{ 33 | android.R.attr.listDivider 34 | }; 35 | private Drawable mDivider; 36 | 37 | private int mOrientation; 38 | 39 | public DividerItemDecoration(Context context, int orientation) { 40 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 41 | mDivider = a.getDrawable(0); 42 | a.recycle(); 43 | setOrientation(orientation); 44 | } 45 | 46 | public void setOrientation(int orientation) { 47 | if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { 48 | throw new IllegalArgumentException("invalid orientation"); 49 | } 50 | mOrientation = orientation; 51 | } 52 | 53 | @Override 54 | public void onDraw(Canvas c, RecyclerView parent) { 55 | if (mOrientation == VERTICAL_LIST) { 56 | drawVertical(c, parent); 57 | } else { 58 | drawHorizontal(c, parent); 59 | } 60 | } 61 | 62 | public void drawVertical(Canvas c, RecyclerView parent) { 63 | final int left = parent.getPaddingLeft(); 64 | final int right = parent.getWidth() - parent.getPaddingRight(); 65 | 66 | final int childCount = parent.getChildCount(); 67 | for (int i = 0; i < childCount; i++) { 68 | final View child = parent.getChildAt(i); 69 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 70 | .getLayoutParams(); 71 | final int top = child.getBottom() + params.bottomMargin + 72 | Math.round(ViewCompat.getTranslationY(child)); 73 | final int bottom = top + mDivider.getIntrinsicHeight(); 74 | mDivider.setBounds(left, top, right, bottom); 75 | mDivider.draw(c); 76 | } 77 | } 78 | 79 | public void drawHorizontal(Canvas c, RecyclerView parent) { 80 | final int top = parent.getPaddingTop(); 81 | final int bottom = parent.getHeight() - parent.getPaddingBottom(); 82 | 83 | final int childCount = parent.getChildCount(); 84 | for (int i = 0; i < childCount; i++) { 85 | final View child = parent.getChildAt(i); 86 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 87 | .getLayoutParams(); 88 | final int left = child.getRight() + params.rightMargin + 89 | Math.round(ViewCompat.getTranslationX(child)); 90 | final int right = left + mDivider.getIntrinsicHeight(); 91 | mDivider.setBounds(left, top, right, bottom); 92 | mDivider.draw(c); 93 | } 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/ui/fragment/MovieSearchFragment.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.ui.fragment; 2 | 3 | import android.app.Fragment; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.text.TextUtils; 9 | import android.util.Log; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | import android.widget.EditText; 14 | import android.widget.FrameLayout; 15 | import android.widget.ProgressBar; 16 | import android.widget.TextView; 17 | 18 | import com.jakewharton.rxbinding2.widget.RxTextView; 19 | import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent; 20 | import com.tonytang.demo.BuildConfig; 21 | import com.tonytang.demo.R; 22 | import com.tonytang.demo.constants.Constants; 23 | import com.tonytang.demo.entity.Movie; 24 | import com.tonytang.demo.model.MoviesWrapper; 25 | import com.tonytang.demo.ui.activity.AndroidApplication; 26 | import com.tonytang.demo.ui.adapter.MovieAdapter; 27 | import com.tonytang.demo.ui.decoration.DividerItemDecoration; 28 | 29 | import java.util.List; 30 | import java.util.concurrent.TimeUnit; 31 | 32 | import butterknife.Bind; 33 | import butterknife.ButterKnife; 34 | import io.reactivex.Observable; 35 | import io.reactivex.Observer; 36 | import io.reactivex.android.schedulers.AndroidSchedulers; 37 | import io.reactivex.disposables.CompositeDisposable; 38 | import io.reactivex.disposables.Disposable; 39 | import io.reactivex.schedulers.Schedulers; 40 | 41 | 42 | public class MovieSearchFragment extends Fragment { 43 | 44 | private static final String TAG = "DebounceSearch"; 45 | //this will be responsible of managing the callback from network request. 46 | protected final CompositeDisposable networkRequestSubscription = new CompositeDisposable(); 47 | 48 | @Bind(R.id.edit_text) 49 | EditText inputSearchText; 50 | @Bind(R.id.recycler_view) 51 | RecyclerView recyclerView; 52 | @Bind(R.id.top_empty_view) 53 | FrameLayout topEmptyView;//It will be shown when the data is loading or the request has error. It will be hiddden if the result is good. 54 | @Bind(R.id.progress_bar) 55 | ProgressBar progressBar; 56 | @Bind(R.id.tv_empty_hint) 57 | TextView tvEmptyViewHint; 58 | 59 | 60 | private Disposable disposable; 61 | private MovieAdapter movieAdapter; 62 | 63 | public static MovieSearchFragment newInstance() { 64 | 65 | return new MovieSearchFragment(); 66 | } 67 | 68 | 69 | @Override 70 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 71 | View layout = inflater.inflate(R.layout.fragment_movie, container, false); 72 | ButterKnife.bind(this, layout); 73 | return layout; 74 | } 75 | 76 | @Override 77 | public void onViewCreated(View view, Bundle savedInstanceState) { 78 | super.onViewCreated(view, savedInstanceState); 79 | recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 80 | recyclerView.setHasFixedSize(true); 81 | recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); 82 | movieAdapter = new MovieAdapter(getActivity()); 83 | recyclerView.setAdapter(movieAdapter); 84 | 85 | 86 | } 87 | 88 | @Override 89 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 90 | super.onActivityCreated(savedInstanceState); 91 | final Observable textViewTextChangeEventObservable = RxTextView.textChangeEvents(inputSearchText).debounce(400, TimeUnit.MILLISECONDS) 92 | .observeOn(AndroidSchedulers.mainThread()); 93 | textViewTextChangeEventObservable.subscribe( 94 | new Observer() { 95 | @Override 96 | public void onSubscribe(Disposable d) { 97 | disposable = d; 98 | } 99 | 100 | @Override 101 | public void onNext(TextViewTextChangeEvent value) { 102 | search(value.text().toString()); 103 | 104 | } 105 | 106 | @Override 107 | public void onError(Throwable e) { 108 | logError(e); 109 | } 110 | 111 | @Override 112 | public void onComplete() { 113 | 114 | } 115 | }); 116 | } 117 | 118 | 119 | private void search(String keyword) { 120 | if (TextUtils.isEmpty(keyword)) { 121 | return; 122 | } 123 | showLoadingView(); 124 | final Observable fetchDataObservable = AndroidApplication.getInstance().getRestClient() 125 | .getMovieService().searchRx(Constants.API_KEY, keyword); 126 | networkRequestSubscription.add(fetchDataObservable.subscribeOn(Schedulers.newThread()) 127 | .observeOn(AndroidSchedulers.mainThread()).subscribe(this::onSuccess, this::onError)); 128 | } 129 | 130 | 131 | private void onSuccess(MoviesWrapper moviesWrapper) { 132 | List results = moviesWrapper.getResults(); 133 | if (results != null) { 134 | if (results.size() > 0) { 135 | movieAdapter.updateDataSet(moviesWrapper.getResults()); 136 | hideEmptyView(); 137 | } else { 138 | showEmptyView(getString(R.string.no_result)); 139 | } 140 | } else { 141 | showEmptyView(getString(R.string.no_result)); 142 | } 143 | 144 | } 145 | 146 | private void hideEmptyView() { 147 | topEmptyView.setVisibility(View.INVISIBLE); 148 | recyclerView.setVisibility(View.VISIBLE); 149 | } 150 | 151 | 152 | private void onError(Throwable throwable) { 153 | showEmptyView(getString(R.string.network_error)); 154 | logError(throwable); 155 | } 156 | 157 | private void showEmptyView(String hint) { 158 | recyclerView.setVisibility(View.INVISIBLE); 159 | progressBar.setVisibility(View.INVISIBLE); 160 | topEmptyView.setVisibility(View.VISIBLE); 161 | tvEmptyViewHint.setText(hint); 162 | tvEmptyViewHint.setVisibility(View.VISIBLE); 163 | } 164 | 165 | 166 | private void showLoadingView() { 167 | recyclerView.setVisibility(View.INVISIBLE); 168 | progressBar.setVisibility(View.VISIBLE); 169 | topEmptyView.setVisibility(View.VISIBLE); 170 | tvEmptyViewHint.setVisibility(View.INVISIBLE); 171 | } 172 | 173 | private void logError(Throwable throwable) { 174 | if (BuildConfig.DEBUG) { 175 | Log.w(TAG, throwable); 176 | } 177 | } 178 | 179 | @Override 180 | public void onDestroyView() { 181 | 182 | networkRequestSubscription.dispose(); 183 | if (disposable != null) { 184 | disposable.dispose(); 185 | } 186 | super.onDestroyView(); 187 | } 188 | 189 | } -------------------------------------------------------------------------------- /app/src/main/java/com/tonytang/demo/util/AppUtils.java: -------------------------------------------------------------------------------- 1 | package com.tonytang.demo.util; 2 | 3 | import android.content.Context; 4 | import android.net.ConnectivityManager; 5 | import android.net.NetworkInfo; 6 | 7 | import com.tonytang.demo.retrofit.service.CacheConfigInterceptor; 8 | import com.tonytang.demo.ui.activity.AndroidApplication; 9 | 10 | /** 11 | * Created by tonythompson on 3/9/16. 12 | */ 13 | public class AppUtils { 14 | 15 | /** 16 | * check the network is connected or not. 17 | *

18 | * Warning: This might not be the best approach to inquiry the network status, 19 | * especially this method will be frequently called in every network request triggered by {@link CacheConfigInterceptor}. 20 | * 21 | * @return true if the device is connected to network or false otherwise. 22 | */ 23 | public static boolean isConnected() { 24 | ConnectivityManager connectivity = (ConnectivityManager) AndroidApplication.getInstance() 25 | .getSystemService(Context.CONNECTIVITY_SERVICE); 26 | if (null != connectivity) { 27 | NetworkInfo info = connectivity.getActiveNetworkInfo(); 28 | if (null != info && info.isConnected()) { 29 | if (info.getState() == NetworkInfo.State.CONNECTED) { 30 | return true; 31 | } 32 | } 33 | } 34 | return false; 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_movie.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 14 | 15 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_movie.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 32 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_empty.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TonyTangAndroid/AndroidDemo/a91a7b8cbac687c906f067859211ea7923f7d590/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TonyTangAndroid/AndroidDemo/a91a7b8cbac687c906f067859211ea7923f7d590/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TonyTangAndroid/AndroidDemo/a91a7b8cbac687c906f067859211ea7923f7d590/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TonyTangAndroid/AndroidDemo/a91a7b8cbac687c906f067859211ea7923f7d590/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TonyTangAndroid/AndroidDemo/a91a7b8cbac687c906f067859211ea7923f7d590/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #ff4e5d64 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 8dp 6 | 8dp 7 | 8 | 9 | 248dp 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Android Demo 3 | Enter any keyword 4 | Network Error 5 | No matched result 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | 4 | //寻找项目依赖的各种jar包以及aar文件的寻找路径。 5 | repositories { 6 | jcenter() 7 | maven { url "https://jitpack.io" } 8 | 9 | } 10 | 11 | //指定各种gradle插件的寻找地址 12 | dependencies { 13 | classpath 'com.android.tools.build:gradle:2.2.3' 14 | classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1' 15 | classpath 'me.tatarka:gradle-retrolambda:3.5.0' 16 | classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.2' 17 | } 18 | } 19 | 20 | 21 | ext.libraries = [recyclerview : "com.android.support:recyclerview-v7:${ANDROID_SUPPORT_VERSION}", 22 | appcompat : "com.android.support:appcompat-v7:${ANDROID_SUPPORT_VERSION}", 23 | cardview : "com.android.support:cardview-v7:${ANDROID_SUPPORT_VERSION}", 24 | design : "com.android.support:design:${ANDROID_SUPPORT_VERSION}", 25 | support_v13 : "com.android.support:support-v13:${ANDROID_SUPPORT_VERSION}", 26 | retrofit : "com.squareup.retrofit2:retrofit:${RETROFIT_VERESION}", 27 | okhttp_urlconnection : "com.squareup.okhttp3:okhttp-urlconnection:${OKHTTP_VERESION}", 28 | okhttp : "com.squareup.okhttp3:okhttp:${OKHTTP_VERESION}", 29 | okhttp_logging_interceptor: "com.squareup.okhttp3:logging-interceptor:${OKHTTP_VERESION}", 30 | converter_gson : "com.squareup.retrofit2:converter-gson:${RETROFIT_VERESION}", 31 | adapter_rxjava2 : "com.squareup.retrofit2:adapter-rxjava2:${RETROFIT_VERESION}", 32 | support_annotations : "com.android.support:support-annotations:${ANDROID_SUPPORT_VERSION}" 33 | 34 | ] 35 | 36 | 37 | 38 | allprojects { 39 | //寻找项目依赖的各种jar包以及aar文件的寻找路径。 40 | repositories { 41 | jcenter() 42 | maven { url "https://jitpack.io" } 43 | } 44 | } 45 | 46 | apply from: 'versioning.gradle' -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.daemon=true 2 | APPPLICATION_ID=com.tonytang.android.demo 3 | VERSION_NAME=2.0.0 4 | VERSION_CODE=10000 5 | APPLICATION_NAME=Android Simple Demo 6 | ANDROID_SUPPORT_VERSION=25.2.0 7 | RETROFIT_VERESION=2.2.0 8 | OKHTTP_VERESION=3.6.0 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TonyTangAndroid/AndroidDemo/a91a7b8cbac687c906f067859211ea7923f7d590/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Feb 24 19:03:18 EST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /versioning.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | /** 3 | * Builds an Android version code from the version of the project. 4 | * This is designed to handle the -SNAPSHOT and -RC format. 5 | * 6 | * I.e. during development the version ends with -SNAPSHOT. As the code stabilizes and release nears 7 | * one or many Release Candidates are tagged. These all end with "-RC1", "-RC2" etc. 8 | * And the final release is without any suffix. 9 | * @return 10 | */ 11 | buildVersionCode = { 12 | //The rules is as follows: 13 | //-SNAPSHOT counts as 0 14 | //-RC* counts as the RC number, i.e. 1 to 98 15 | //final release counts as 99. 16 | //Thus you can only have 98 Release Candidates, which ought to be enough for everyone 17 | 18 | def candidate = "99" 19 | def (major, minor, patch) = VERSION_NAME.toLowerCase().replaceAll('-', '').tokenize('.') 20 | if (patch.endsWith("debug")) { 21 | candidate = "0" 22 | patch = patch.replaceAll("[^0-9]","") 23 | } else { 24 | def rc 25 | (patch, rc) = patch.tokenize("rc") 26 | if (rc) { 27 | candidate = rc 28 | } 29 | } 30 | 31 | (major, minor, patch, candidate) = [major, minor, patch, candidate].collect{it.toInteger()} 32 | 33 | (major * 1000000) + (minor * 10000) + (patch * 100) + candidate; 34 | } 35 | } --------------------------------------------------------------------------------