Skip to content

Commit 3bbfbad

Browse files
committed
FEAT : Better handling of RTL support
1 parent 85197a9 commit 3bbfbad

8 files changed

Lines changed: 69 additions & 53 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<manifest package="com.matpag.ui.clickdrawable"/>
1+
<manifest package="com.matpag.clickdrawabletextview"/>

library/src/main/java/com/matpag/clickdrawabletextview/ClickDrawableAutoCompleteTextView.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ public void removeAllCsDrawables() {
8888
mCsDrawableViewManager.removeAllCsDrawables();
8989
}
9090

91-
@Override
92-
public void enableRTL(boolean enable) {
93-
mCsDrawableViewManager.enableRTL(enable);
94-
}
9591

9692
@Override
9793
public void disableFocusOnText(boolean preventReFocus, boolean closeKeyboard) {

library/src/main/java/com/matpag/clickdrawabletextview/ClickDrawableEditText.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,4 @@ public void enableFocusOnText(boolean openKeyboard) {
9797
mCsDrawableViewManager.enableFocusOnText(openKeyboard);
9898
}
9999

100-
@Override
101-
public void enableRTL(boolean enable) {
102-
mCsDrawableViewManager.enableRTL(enable);
103-
}
104100
}

library/src/main/java/com/matpag/clickdrawabletextview/ClickDrawableTextView.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,4 @@ public void removeAllCsDrawables() {
9797
mCsDrawableViewManager.removeAllCsDrawables();
9898
}
9999

100-
@Override
101-
public void enableRTL(boolean enable) {
102-
mCsDrawableViewManager.enableRTL(enable);
103-
}
104100
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.matpag.clickdrawabletextview;
2+
3+
import android.content.Context;
4+
import android.content.pm.ApplicationInfo;
5+
import android.content.pm.PackageManager;
6+
7+
/**
8+
* Global configuration to handle correctly some user specific choices
9+
*
10+
* Created by Mattia Pagini on 17/05/2017.
11+
*/
12+
public class CsDrawableSettings {
13+
14+
private boolean rtlSupportEnabled;
15+
16+
private static CsDrawableSettings mSettings;
17+
18+
private CsDrawableSettings(boolean rtlSupportEnabled){
19+
this.rtlSupportEnabled = rtlSupportEnabled;
20+
}
21+
22+
/**
23+
* Init method to call before every custom view initialization, this should preferably be
24+
* called in the custom {@link android.app.Application} app class. But you can use is in the
25+
* activity too before calling {@link android.app.Activity#setContentView(int)} or calling
26+
* this per Activity if you need to support RTL in some activities and not in others
27+
* @param context the app context
28+
* @param packageName the packageName of the app, <code>BuildConfig.APPLICATION_ID</code> should
29+
* be the proper choice in the most cases
30+
*/
31+
public static void init(Context context, String packageName){
32+
PackageManager pManager = context.getApplicationContext().getPackageManager();
33+
try {
34+
ApplicationInfo appInfo = pManager.getApplicationInfo(packageName, 0);
35+
//read the Application android:supportsRtl xml properties (if present), default false
36+
boolean rtlSupport = (appInfo.flags & ApplicationInfo.FLAG_SUPPORTS_RTL) != 0;
37+
mSettings = new CsDrawableSettings(rtlSupport);
38+
} catch (PackageManager.NameNotFoundException nfe){
39+
throw new IllegalArgumentException("Unable to get info for the provided packageName, " +
40+
"are you sure is it correct? BuildConfig.APPLICATION_ID should be fine " +
41+
"for the most cases");
42+
}
43+
}
44+
45+
/**
46+
* Expose the RTL support flag
47+
* @return true if the developer added <code>android:supportsRtl="true"</code> to the
48+
* application manifest, false otherwise
49+
*/
50+
static boolean isRtlSupportEnabled(){
51+
if (mSettings == null){
52+
throw new NullPointerException("You need to call CsDrawableSettings.init() in your " +
53+
"custom Application class or Activity before using the library");
54+
}
55+
return mSettings.rtlSupportEnabled;
56+
}
57+
58+
}

library/src/main/java/com/matpag/clickdrawabletextview/CsDrawableViewManager.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import android.view.inputmethod.InputMethodManager;
1414
import android.widget.TextView;
1515

16-
import com.matpag.ui.clickdrawable.R;
1716
import com.matpag.clickdrawabletextview.interfaces.ClickableDrawable;
1817
import com.matpag.clickdrawabletextview.interfaces.OnDrawableClickListener;
1918

@@ -44,9 +43,6 @@ final class CsDrawableViewManager implements ClickableDrawable {
4443
//default true
4544
private boolean enableTouchOnText = true;
4645

47-
//default true
48-
private boolean enableRTL = true;
49-
5046
private Configuration mConfig;
5147

5248
/**
@@ -152,9 +148,6 @@ void init(Context context, AttributeSet attrs) {
152148
mBottomDrawable.setVisibility(visibility);
153149
}
154150

155-
enableRTL = a.getBoolean(R.styleable.CsDrawableViewManager_csEnableRTL,
156-
true);
157-
158151
a.recycle();
159152
}
160153

@@ -164,8 +157,8 @@ void init(Context context, AttributeSet attrs) {
164157
}
165158

166159
/**
167-
* Check if the current Locale is in RTL mode and if the user has enabled the view to use it
168-
* with {@link #enableRTL(boolean)}
160+
* Check if the current Locale is in RTL mode and if the user has enabled the view to support
161+
* it in the <code>AndroidManifest.xml</code> of his app
169162
*
170163
* NOTE: We should use <code>ViewCompat.getLayoutDirection(view) ==
171164
* ViewCompat.LAYOUT_DIRECTION_RTL</code> but when you use developer option : Force RTL Layout
@@ -177,7 +170,8 @@ void init(Context context, AttributeSet attrs) {
177170
* @return true if in RTL, false otherwise
178171
*/
179172
private boolean isLayoutRTL(){
180-
return mConfig.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL && enableRTL;
173+
return CsDrawableSettings.isRtlSupportEnabled() &&
174+
mConfig.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
181175
}
182176

183177
/**
@@ -206,8 +200,7 @@ private void addCompoundDrawables(){
206200

207201
/**
208202
* Add the compound drawables to the view, if the Locale of the user is in
209-
* RTL mode and {@link #enableRTL} is true the drawable will be added
210-
* in the proper position
203+
* RTL mode the drawables will be added in the proper position
211204
*/
212205
private void addCompoundDrawablesRelative(){
213206
view.setCompoundDrawablesRelative(
@@ -369,12 +362,6 @@ public void showBottomCsDrawable(boolean visible) {
369362
}
370363
}
371364

372-
@Override
373-
public void enableRTL(boolean enable) {
374-
enableRTL = enable;
375-
invalidateDrawables();
376-
}
377-
378365
@Override
379366
public void disableFocusOnText(boolean preventReFocus, boolean closeKeyboard) {
380367
//block refocus on others edittext

library/src/main/java/com/matpag/clickdrawabletextview/DrawablePosition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.matpag.clickdrawabletextview;
22

33
/**
4+
* Enum position
45
* Created by Mattia Pagini on 13/02/2017.
56
*/
67

library/src/main/java/com/matpag/clickdrawabletextview/interfaces/ClickableDrawable.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public interface ClickableDrawable {
2525

2626
/**
2727
* Add the start drawable to the view, default the LEFT drawable.
28-
* If {@link #enableRTL(boolean)} with <code>true</code> is called, this could be the
29-
* LEFT or RIGHT drawable based on the user locale
28+
* this could be the LEFT or RIGHT drawable based on the user locale if the developer
29+
* has added <code>android:supportsRtl="true"</code> in his <code>AndroidManifest.xml</code>
3030
* @param csDrawable the CsDrawable object (can be null)
3131
*/
3232
void addStartCsDrawable(CsDrawable csDrawable);
@@ -39,8 +39,8 @@ public interface ClickableDrawable {
3939

4040
/**
4141
* Add the end drawable to the view, default the RIGHT drawable.
42-
* If {@link #enableRTL(boolean)} with <code>true</code> is called, this could be the
43-
* LEFT or RIGHT drawable based on the user locale
42+
* this could be the LEFT or RIGHT drawable based on the user locale if the developer
43+
* has added <code>android:supportsRtl="true"</code> in his <code>AndroidManifest.xml</code>
4444
* @param csDrawable the CsDrawable object (can be null)
4545
*/
4646
void addEndCsDrawable(CsDrawable csDrawable);
@@ -80,24 +80,6 @@ public interface ClickableDrawable {
8080
*/
8181
void removeAllCsDrawables();
8282

83-
/**
84-
* <p>
85-
* Enable the support for RTL languagues, obviously you should have setted the properties
86-
* <code>android:supportsRtl="true"</code> in the AndroidManifest.xml for the locales suporting
87-
* this convention.
88-
* </p>
89-
*
90-
* <p>
91-
* Enabling this means that your {@link CsDrawable} objects in START and END position could
92-
* be inverted in some Locale
93-
* </p>
94-
*
95-
* @param enable <code>true</code> to enable support to <code>RTL</code> locales,
96-
* <code>false</code> to block everything in <code>LTR</code>.
97-
* default: false
98-
*/
99-
void enableRTL(boolean enable);
100-
10183
/**
10284
* <p>
10385
* Disable focus on the view, simulating a {@link android.view.View#setEnabled(boolean)}

0 commit comments

Comments
 (0)