Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Added ability to refresh publisher status #6301

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,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 @@ -671,5 +684,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 @@ -32,4 +32,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 @@ -1167,47 +1167,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 @@ -1678,4 +1639,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 @@ -762,6 +762,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 @@ -179,6 +179,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 @@ -256,6 +261,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 @@ -1299,6 +1299,9 @@ until they verify, or until 90 days have passed.
<message name="IDS_DATA_HAS_BEEN_CLEARED" desc="Brave stats pref summary text">
Privacy reports data has been cleared.
</message>
<message name="IDS_BRAVE_UI_REFRESH_PUBLISHER_STATUS" desc="A button that allows to refresh publisher status">
Refresh Status
</message>
</messages>
</release>
</grit>