Skip to content

Commit

Permalink
[Android] Added ability to refresh publisher status
Browse files Browse the repository at this point in the history
  • Loading branch information
samartnik committed Jul 31, 2020
1 parent ede04c7 commit fcb2465
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,19 @@ public void OnRecoverWallet(int errorCode) {
}
}

public void RefreshPublisher(String publisherKey) {
synchronized (lock) {
nativeRefreshPublisher(mNativeBraveRewardsNativeWorker, publisherKey);
}
}

@CalledByNative
public void OnRefreshPublisher(int status, String publisherKey) {
for (BraveRewardsObserver observer : mObservers) {
observer.OnRefreshPublisher(status, publisherKey);
}
}

@CalledByNative
public void OnGetRewardsMainEnabled(boolean enabled) {
int oldRewardsStatus = rewardsStatus;
Expand Down Expand Up @@ -685,5 +698,6 @@ private native boolean nativeIsCurrentPublisherInRecurrentDonations(long nativeB
private native void nativeDisconnectWallet(long nativeBraveRewardsNativeWorker, String wallet_type);
private native void nativeProcessRewardsPageUrl(long nativeBraveRewardsNativeWorker, String path, String query);
private native void nativeRecoverWallet(long nativeBraveRewardsNativeWorker, String passPhrase);
private native void nativeRefreshPublisher(long nativeBraveRewardsNativeWorker, String publisherKey);
private native void nativeGetRewardsParameters(long nativeBraveRewardsNativeWorker);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ default public void OnProcessRewardsPageUrl(int error_code,
String wallet_type, String action, String json_args ) {};
default public void OnClaimPromotion(int error_code) {};
default public void OnRecoverWallet(int errorCode) {};
default public void OnRefreshPublisher(int status, String publisherKey){};
}
133 changes: 92 additions & 41 deletions android/java/org/chromium/chrome/browser/BraveRewardsPanelPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -1229,47 +1229,8 @@ public void OnPublisherInfo(int tabId) {
btAutoContribute.setOnCheckedChangeListener(autoContributeSwitchListener);
}

// set publisher verified/unverified status
String verified_text = "";
TextView tvVerified = (TextView)root.findViewById(R.id.publisher_verified);
@PublisherStatus int pubStatus =
thisObject.mBraveRewardsNativeWorker.GetPublisherStatus(currentTabId);
if (pubStatus == BraveRewardsPublisher.CONNECTED ||
pubStatus == BraveRewardsPublisher.VERIFIED) {
verified_text = root.getResources().getString(R.string.brave_ui_verified_publisher);
} else {
verified_text = root.getResources().getString(R.string.brave_ui_not_verified_publisher);
tvVerified.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bat_unverified, 0, 0, 0);
}
tvVerified.setText(verified_text);
tvVerified.setVisibility(View.VISIBLE);

// show |brave_ui_panel_connected_text| text if
// publisher is CONNECTED and user doesn't have any Brave funds (anonymous or
// blinded wallets)
String verified_description = "";
if (pubStatus == BraveRewardsPublisher.CONNECTED) {
BraveRewardsBalance balance_obj = mBraveRewardsNativeWorker.GetWalletBalance();
if (balance_obj != null) {
double braveFunds = ((balance_obj.mWallets.containsKey(BraveRewardsBalance.WALLET_ANONYMOUS) && balance_obj.mWallets.get(BraveRewardsBalance.WALLET_ANONYMOUS) != null) ? balance_obj.mWallets.get(BraveRewardsBalance.WALLET_ANONYMOUS) : .0) +
((balance_obj.mWallets.containsKey(BraveRewardsBalance.WALLET_BLINDED) && balance_obj.mWallets.get(BraveRewardsBalance.WALLET_BLINDED) != null) ? balance_obj.mWallets.get(BraveRewardsBalance.WALLET_BLINDED) : .0);
if (braveFunds <= 0) {
verified_description = root.getResources().getString(R.string.brave_ui_panel_connected_text);
}
}
} else if (pubStatus == BraveRewardsPublisher.NOT_VERIFIED) {
verified_description = root.getResources().getString(
R.string.brave_ui_not_verified_publisher_description);
}

if (!TextUtils.isEmpty(verified_description)) {
verified_description += "<br/><font color=#73CBFF>" +
root.getResources().getString(R.string.learn_more) + ".</font>";
Spanned toInsert = BraveRewardsHelper.spannedFromHtmlString(verified_description);
TextView tv_note = (TextView)root.findViewById(R.id.publisher_not_verified);
tv_note.setText(toInsert);
tv_note.setVisibility(View.VISIBLE);
}
UpdatePublisherStatus(
thisObject.mBraveRewardsNativeWorker.GetPublisherStatus(currentTabId));

tv = (TextView)root.findViewById(R.id.br_no_activities_yet);
gl = (GridLayout)thisObject.root.findViewById(R.id.br_activities);
Expand Down Expand Up @@ -1740,4 +1701,94 @@ public void OnClaimPromotion(int error_code) {
ShowNotification(REWARDS_PROMOTION_CLAIM_ERROR_ID, REWARDS_PROMOTION_CLAIM_ERROR, 0, args);
}
}

private void UpdatePublisherStatus(int pubStatus) {
// Set publisher verified/unverified status
String verified_text = "";
TextView publisherVerified = (TextView) root.findViewById(R.id.publisher_verified);
publisherVerified.setAlpha(1f);
TextView publisherDelimiter = (TextView) root.findViewById(R.id.publisher_delimiter);
publisherDelimiter.setAlpha(1f);
publisherDelimiter.setText(" | ");
TextView refreshPublisher = (TextView) root.findViewById(R.id.refresh_publisher);
refreshPublisher.setAlpha(1f);
refreshPublisher.setEnabled(true);
View refreshStatusProgress = root.findViewById(R.id.progress_refresh_status);
refreshStatusProgress.setVisibility(View.GONE);
refreshPublisher.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
String pubId = thisObject.mBraveRewardsNativeWorker.GetPublisherId(currentTabId);
refreshStatusProgress.setVisibility(View.VISIBLE);
refreshPublisher.setEnabled(false);
publisherVerified.setAlpha(.3f);
publisherDelimiter.setAlpha(.3f);
refreshPublisher.setAlpha(.3f);
mBraveRewardsNativeWorker.RefreshPublisher(pubId);
}
}));
if (pubStatus == BraveRewardsPublisher.CONNECTED
|| pubStatus == BraveRewardsPublisher.VERIFIED) {
verified_text = root.getResources().getString(R.string.brave_ui_verified_publisher);
publisherVerified.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.bat_verified, 0, 0, 0);
publisherDelimiter.setVisibility(View.GONE);
refreshPublisher.setVisibility(View.GONE);
} else {
verified_text = root.getResources().getString(R.string.brave_ui_not_verified_publisher);
publisherVerified.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.bat_unverified, 0, 0, 0);
publisherDelimiter.setVisibility(View.VISIBLE);
refreshPublisher.setVisibility(View.VISIBLE);
}
publisherVerified.setText(verified_text);
publisherVerified.setVisibility(View.VISIBLE);

// show |brave_ui_panel_connected_text| text if
// publisher is CONNECTED and user doesn't have any Brave funds (anonymous or
// blinded wallets)
String verified_description = "";
if (pubStatus == BraveRewardsPublisher.CONNECTED) {
BraveRewardsBalance balance_obj = mBraveRewardsNativeWorker.GetWalletBalance();
if (balance_obj != null) {
double braveFunds =
((balance_obj.mWallets.containsKey(BraveRewardsBalance.WALLET_ANONYMOUS)
&& balance_obj.mWallets.get(BraveRewardsBalance.WALLET_ANONYMOUS)
!= null)
? balance_obj.mWallets.get(
BraveRewardsBalance.WALLET_ANONYMOUS)
: .0)
+ ((balance_obj.mWallets.containsKey(BraveRewardsBalance.WALLET_BLINDED)
&& balance_obj.mWallets.get(BraveRewardsBalance.WALLET_BLINDED)
!= null)
? balance_obj.mWallets.get(
BraveRewardsBalance.WALLET_BLINDED)
: .0);
if (braveFunds <= 0) {
verified_description =
root.getResources().getString(R.string.brave_ui_panel_connected_text);
}
}
} else if (pubStatus == BraveRewardsPublisher.NOT_VERIFIED) {
verified_description = root.getResources().getString(
R.string.brave_ui_not_verified_publisher_description);
}

if (!TextUtils.isEmpty(verified_description)) {
verified_description += "<br/><font color=#73CBFF>"
+ root.getResources().getString(R.string.learn_more) + ".</font>";
Spanned toInsert = BraveRewardsHelper.spannedFromHtmlString(verified_description);
TextView tv_note = (TextView) root.findViewById(R.id.publisher_not_verified);
tv_note.setText(toInsert);
tv_note.setVisibility(View.VISIBLE);
}
}

@Override
public void OnRefreshPublisher(int status, String publisherKey) {
String pubName = thisObject.mBraveRewardsNativeWorker.GetPublisherName(currentTabId);
if (pubName.equals(publisherKey)) {
UpdatePublisherStatus(status);
}
};
}
71 changes: 59 additions & 12 deletions android/java/res/layout/brave_rewards_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,66 @@
android:textColor="@color/modern_grey_700"
android:textSize="16sp"/>

<TextView
android:id="@+id/publisher_verified"
android:layout_height="wrap_content"
<FrameLayout
android:layout_width="wrap_content"
android:layout_gravity="start|center"
android:text="@string/brave_ui_verified_publisher"
android:drawableStart="@drawable/bat_verified"
android:drawablePadding="4dp"
android:fontFamily="sans-serif"
android:gravity="start"
android:textSize="14sp"
android:textColor="@color/modern_grey_700"
android:visibility="gone"/>
android:layout_height="wrap_content"
android:background="@android:color/transparent" >

<ProgressBar
android:id="@+id/progress_refresh_status"
android:indeterminateDrawable="@drawable/progress_indeterminate_orange"
android:layout_height="18dp"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:alpha="1"
android:visibility="gone" />

<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/publisher_verified"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="start|center"
android:text="@string/brave_ui_verified_publisher"
android:drawableStart="@drawable/bat_verified"
android:drawablePadding="4dp"
android:fontFamily="sans-serif"
android:gravity="start"
android:textSize="14sp"
android:textColor="@color/modern_grey_700"
android:visibility="gone" />

<TextView
android:id="@+id/publisher_delimiter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="start|center"
android:text=""
android:fontFamily="sans-serif"
android:textSize="14sp"
android:textColor="@color/modern_grey_700"
android:visibility="gone" />

<TextView
android:id="@+id/refresh_publisher"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="start|center"
android:text="@string/brave_ui_refresh_publisher_status"
android:fontFamily="sans-serif"
android:textSize="14sp"
android:textColor="#4E56CD"
android:clickable="true"
android:visibility="gone" />

</LinearLayout>

</FrameLayout>

</LinearLayout>

Expand Down
23 changes: 23 additions & 0 deletions browser/brave_rewards/android/brave_rewards_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,29 @@ void BraveRewardsNativeWorker::OnRecoverWallet(
env, weak_java_brave_rewards_native_worker_.get(env), result);
}

void BraveRewardsNativeWorker::RefreshPublisher(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& publisher_key) {
if (!brave_rewards_service_) {
NOTREACHED();
return;
}
brave_rewards_service_->RefreshPublisher(
base::android::ConvertJavaStringToUTF8(env, publisher_key),
base::BindOnce(&BraveRewardsNativeWorker::OnRefreshPublisher,
base::Unretained(this)));
}

void BraveRewardsNativeWorker::OnRefreshPublisher(
uint32_t status,
const std::string& publisher_key) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_BraveRewardsNativeWorker_OnRefreshPublisher(
env, weak_java_brave_rewards_native_worker_.get(env), status,
base::android::ConvertUTF8ToJavaString(env, publisher_key));
}

static void JNI_BraveRewardsNativeWorker_Init(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller) {
Expand Down
7 changes: 7 additions & 0 deletions browser/brave_rewards/android/brave_rewards_native_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ class BraveRewardsNativeWorker : public brave_rewards::RewardsServiceObserver,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& pass_phrase);

void RefreshPublisher(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& publisher_key);

void OnResetTheWholeState(const bool success);

void OnGetGetReconcileStamp(uint64_t timestamp);
Expand Down Expand Up @@ -263,6 +268,8 @@ class BraveRewardsNativeWorker : public brave_rewards::RewardsServiceObserver,
brave_rewards::RewardsService* rewards_service,
const int32_t result) override;

void OnRefreshPublisher(uint32_t status, const std::string& publisher_key);

private:
std::string StdStrStrMapToJsonString(
const std::map<std::string, std::string>& args);
Expand Down
3 changes: 3 additions & 0 deletions browser/ui/android/strings/android_brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,9 @@ until they verify, or until 90 days have passed.
<message name="IDS_NOTIFICATION_BRAVE_STATS_TIME" desc="Rewards notification text">
Nice! You've saved 10 Mins of loading time with Brave browser.
</message>
<message name="IDS_BRAVE_UI_REFRESH_PUBLISHER_STATUS" desc="A button that allows to refresh publisher status">
Refresh Status
</message>
</messages>
</release>
</grit>

0 comments on commit fcb2465

Please sign in to comment.