Skip to content

Commit 0a04c83

Browse files
authored
Merge pull request #13 from flipkart-incubator/checks
code cleanup, added precondition check class
2 parents b106476 + 8ca018c commit 0a04c83

File tree

9 files changed

+47
-35
lines changed

9 files changed

+47
-35
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
google()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.1.1'
9+
classpath 'com.android.tools.build:gradle:3.1.4'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

inline-youtube-view/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ buildscript {
2929
google()
3030
}
3131
dependencies {
32-
classpath 'com.android.tools.build:gradle:3.1.1'
33-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
34-
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3'
32+
classpath 'com.android.tools.build:gradle:3.1.4'
33+
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
34+
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.7.5'
3535
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
3636
}
3737
}

inline-youtube-view/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

inline-youtube-view/src/main/java/com/flipkart/youtubeview/YouTubePlayerView.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.flipkart.youtubeview.listener.YouTubeEventListener;
4343
import com.flipkart.youtubeview.models.ImageLoader;
4444
import com.flipkart.youtubeview.models.YouTubePlayerType;
45+
import com.flipkart.youtubeview.util.$Precondition$Check;
4546
import com.flipkart.youtubeview.util.ServiceUtil;
4647

4748
public class YouTubePlayerView extends FrameLayout {
@@ -82,7 +83,6 @@ public YouTubePlayerView(Context context, AttributeSet attrs, int defStyleAttr)
8283
@Override
8384
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
8485
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
85-
8686
int newWidth;
8787
int newHeight;
8888
newWidth = getMeasuredWidth();
@@ -103,19 +103,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
103103
@MainThread
104104
public void initPlayer(@NonNull String apiKey, @NonNull String videoId, @Nullable String webViewUrl, @YouTubePlayerType int playerType,
105105
@Nullable YouTubeEventListener listener, @NonNull Fragment fragment, @NonNull ImageLoader imageLoader) {
106-
if (TextUtils.isEmpty(videoId) || TextUtils.isEmpty(apiKey)) {
107-
throw new IllegalArgumentException("Video Id or key cannot be null");
108-
}
109-
106+
$Precondition$Check.checkArgument(!TextUtils.isEmpty(apiKey), "apiKey cannot be null");
107+
$Precondition$Check.checkArgument(!TextUtils.isEmpty(videoId), "videoId cannot be null");
110108
//noinspection ConstantConditions
111-
if (fragment == null) {
112-
throw new IllegalArgumentException("Fragment cannot be null");
113-
}
114-
109+
$Precondition$Check.checkArgument(fragment != null, "Fragment cannot be null");
115110
//noinspection ConstantConditions
116-
if (imageLoader == null) {
117-
throw new IllegalArgumentException("ImageLoader cannot be null");
118-
}
111+
$Precondition$Check.checkArgument(imageLoader != null, "ImageLoader cannot be null");
119112

120113
this.key = apiKey;
121114
this.videoId = videoId;
@@ -204,9 +197,7 @@ private void attachPlayer(boolean isNative) {
204197
}
205198
youtubePlayerFragment = webViewFragment;
206199
}
207-
208200
youtubePlayerFragment.setYouTubeEventListener(listener);
209-
210201
this.fragment.getChildFragmentManager().beginTransaction().add(R.id.youtubeFragmentContainer, (Fragment) youtubePlayerFragment, TAG)
211202
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
212203
.commit();

inline-youtube-view/src/main/java/com/flipkart/youtubeview/activity/YouTubeActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.flipkart.youtubeview.R;
3838
import com.flipkart.youtubeview.listener.YouTubeEventListener;
3939
import com.flipkart.youtubeview.models.PlayerStateList;
40+
import com.flipkart.youtubeview.util.$Precondition$Check;
4041
import com.flipkart.youtubeview.util.ServiceUtil;
4142
import com.flipkart.youtubeview.webview.YouTubePlayerWebView;
4243
import com.google.android.youtube.player.YouTubeBaseActivity;
@@ -86,9 +87,8 @@ protected void onCreate(Bundle savedInstanceState) {
8687
* In case videoId or apiKey is null, throw IllegalStateException as apiKey and videoId is mandatory to run
8788
* youtube activity.
8889
*/
89-
if (TextUtils.isEmpty(videoId) || TextUtils.isEmpty(apiKey)) {
90-
throw new IllegalStateException("VideoId and ApiKey cannot be null.");
91-
}
90+
$Precondition$Check.checkArgument(!TextUtils.isEmpty(videoId), " videoId cannot be null");
91+
$Precondition$Check.checkArgument(!TextUtils.isEmpty(apiKey), " apiKey cannot be null");
9292

9393
/*
9494
* In case of YouTube Service not available, fallback to WebView implementation.

inline-youtube-view/src/main/java/com/flipkart/youtubeview/fragment/YouTubeFragment.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.flipkart.youtubeview.listener.YouTubeEventListener;
2828
import com.flipkart.youtubeview.models.PlayerStateList;
29+
import com.flipkart.youtubeview.util.$Precondition$Check;
2930
import com.google.android.youtube.player.YouTubeInitializationResult;
3031
import com.google.android.youtube.player.YouTubePlayer;
3132
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
@@ -43,15 +44,14 @@ public final class YouTubeFragment extends YouTubePlayerSupportFragment implemen
4344
private YouTubePlayer youTubePlayer;
4445

4546
public static YouTubeFragment newInstance(@NonNull String apiKey, @NonNull final String videoId) {
46-
if (TextUtils.isEmpty(apiKey) || TextUtils.isEmpty(videoId)) {
47-
throw new IllegalStateException("VideoId and ApiKey cannot be null.");
48-
}
49-
YouTubeFragment youTubeFragment = new YouTubeFragment();
47+
$Precondition$Check.checkArgument(!TextUtils.isEmpty(apiKey), "apiKey cannot be null");
48+
$Precondition$Check.checkArgument(!TextUtils.isEmpty(videoId), "videoId cannot be null");
49+
YouTubeFragment fragment = new YouTubeFragment();
5050
Bundle bundle = new Bundle();
5151
bundle.putString(ARG_VIDEO_ID, videoId);
5252
bundle.putString(ARG_API_KEY, apiKey);
53-
youTubeFragment.setArguments(bundle);
54-
return youTubeFragment;
53+
fragment.setArguments(bundle);
54+
return fragment;
5555
}
5656

5757
@Override

inline-youtube-view/src/main/java/com/flipkart/youtubeview/fragment/YouTubeWebViewFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public final class YouTubeWebViewFragment extends Fragment implements YouTubeEve
4141
private YouTubeEventListener youTubeEventListener;
4242

4343
public static YouTubeWebViewFragment newInstance(@NonNull String webViewUrl, @NonNull String videoId) {
44-
YouTubeWebViewFragment youTubeWebViewFragment = new YouTubeWebViewFragment();
44+
YouTubeWebViewFragment fragment = new YouTubeWebViewFragment();
4545
Bundle bundle = new Bundle();
4646
bundle.putString(WEB_VIEW_URL, webViewUrl);
4747
bundle.putString(VIDEO_ID, videoId);
48-
youTubeWebViewFragment.setArguments(bundle);
49-
return youTubeWebViewFragment;
48+
fragment.setArguments(bundle);
49+
return fragment;
5050
}
5151

5252
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.flipkart.youtubeview.util;
2+
3+
public final class $Precondition$Check {
4+
public static void checkArgument(boolean expression) {
5+
if (!expression) {
6+
throw new IllegalArgumentException();
7+
}
8+
}
9+
10+
/**
11+
* Ensures that an expression checking an argument is true.
12+
*
13+
* @param expression the expression to check
14+
* @param errorMessage the exception message to use if the check fails; will
15+
* be converted to a string using {@link String#valueOf(Object)}
16+
* @throws IllegalArgumentException if {@code expression} is false
17+
*/
18+
public static void checkArgument(boolean expression, final Object errorMessage) {
19+
if (!expression) {
20+
throw new IllegalArgumentException(String.valueOf(errorMessage));
21+
}
22+
}
23+
}

inline-youtube-view/src/main/java/com/flipkart/youtubeview/webview/YouTubePlayerWebView.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.flipkart.youtubeview.BuildConfig;
3838
import com.flipkart.youtubeview.listener.YouTubeEventListener;
3939
import com.flipkart.youtubeview.models.PlayerStateList;
40+
import com.flipkart.youtubeview.util.$Precondition$Check;
4041

4142
public class YouTubePlayerWebView extends WebView {
4243

@@ -76,10 +77,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
7677
}
7778

7879
public void initialize(@NonNull String webViewUrl) {
79-
if (TextUtils.isEmpty(webViewUrl)) {
80-
throw new IllegalArgumentException("WebView url cannot be empty");
81-
}
82-
80+
$Precondition$Check.checkArgument(!TextUtils.isEmpty(webViewUrl), "WebView url cannot be empty");
8381
if (!isPlayerReady) {
8482
initWebView(webViewUrl);
8583
}

0 commit comments

Comments
 (0)