Expert: Integration of HMS Core Kits in Expense Android App

classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
classpath 'com.google.gms:google-services:4.3.5'
//HMS Kits
api 'com.huawei.hms:dynamicability:1.0.11.302'
implementation 'com.huawei.agconnect:agconnect-auth:1.4.1.300'
implementation 'com.huawei.hms:hwid:5.3.0.302'
implementation 'com.huawei.hms:ads-lite:13.4.30.307'
implementation 'com.huawei.agconnect:agconnect-remoteconfig:1.6.0.300'
implementation 'com.huawei.hms:hianalytics:5.0.3.300'
implementation 'com.huawei.agconnect:agconnect-crash:1.4.1.300'
package com.hms.expensedemo.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.hms.expensedemo.R;
import com.huawei.agconnect.crash.AGConnectCrash;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.ads.AdListener;
import com.huawei.hms.ads.AdParam;
import com.huawei.hms.ads.InterstitialAd;
import com.huawei.hms.ads.banner.BannerView;
import com.huawei.hms.analytics.HiAnalytics;
import com.huawei.hms.analytics.HiAnalyticsInstance;
import com.huawei.hms.analytics.HiAnalyticsTools;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.support.hwid.HuaweiIdAuthManager;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParams;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParamsHelper;
import com.huawei.hms.support.hwid.result.AuthHuaweiId;
import com.huawei.hms.support.hwid.service.HuaweiIdAuthService;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

private static final int REQUEST_SIGN_IN_LOGIN = 1002;
private static String TAG = LoginActivity.class.getName();
private HuaweiIdAuthService mAuthManager;
private HuaweiIdAuthParams mAuthParam;
private BannerView hwBannerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button view = findViewById(R.id.btn_sign);
view.setOnClickListener(this);

AGConnectCrash.getInstance().enableCrashCollection(false);
//Crash application
AGConnectCrash.getInstance().testIt(this);
initAds();
}


private void loadFullScreenAds(){
interstitialAd = new InterstitialAd(getActivity());
interstitialAd.setAdId("testb4znbuh3n2");
AdParam adParam = new AdParam.Builder().build();
interstitialAd.loadAd(adParam);
interstitialAd.setAdListener(adListener); interstitialAd.show(); private AdListener adListener = new AdListener() {
@Override
public void onAdLoaded() {
Log.d(TAG, "onAdLoaded");
showInterstitialAd();
}

@Override
public void onAdFailed(int errorCode) {
Log.d(TAG, "onAdFailed");
}

@Override
public void onAdOpened() {
Log.d(TAG, "onAdOpened");
}

@Override
public void onAdClicked() {
Log.d(TAG, "onAdClicked");
}

@Override
public void onAdLeave() {
Log.d(TAG, "onAdLeave");
}

@Override
public void onAdClosed() {
Log.d(TAG, "onAdClosed");
}
};

}

private void initAds() {
HwAds.init(this);
hwBannerView = findViewById(R.id.huawei_banner_view);
hwBannerView.setVisibility(View.VISIBLE);
AdParam adParam = new AdParam.Builder().build();
hwBannerView.loadAd(adParam);
hwBannerView.setAdListener(adListener);

}

private void signIn() {
mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.createParams();
mAuthManager = HuaweiIdAuthManager.getService(this, mAuthParam);
startActivityForResult(mAuthManager.getSignInIntent(), REQUEST_SIGN_IN_LOGIN);
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_sign) {
signIn();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SIGN_IN_LOGIN) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.i(TAG, huaweiAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken: " + huaweiAccount.getAccessToken());

Bundle bundle = new Bundle();
bundle.putString(TAG, huaweiAccount.getDisplayName() + " signIn success ");
String eventName = "Login";

bundle.putDouble("ID", 999);
bundle.putLong("Details", 100L);
Analystics.getInstance(this).setEvent("login", bundle);

HiAnalyticsInstance instance = HiAnalytics.getInstance(this);
HiAnalyticsTools.enableLog();


if (instance != null) {
instance.onEvent(eventName, bundle);
}

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("user", huaweiAccount.getDisplayName());
startActivity(intent);
this.finish();

} else {
Log.i(TAG, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
}
}

}
}
package com.hms.expensedemo.activities;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;

import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;

import com.hms.expensedemo.R;
import com.hms.expensedemo.adapters.SectionsPageAdapter;
import com.hms.expensedemo.fragments.BalanceFragment;
import com.hms.expensedemo.fragments.CustomBottomSheetDialogFragment;
import com.hms.expensedemo.fragments.ExpenseFragment;

public class MainActivity extends AppCompatActivity {

private ViewPager mViewPager;

public static FloatingActionButton fab;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mViewPager=findViewById(R.id.container);
setupViewPager(mViewPager);

TabLayout tabLayout=findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);


fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");

}
});

}




private void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter=new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new ExpenseFragment(),"Expenses");
adapter.addFragment(new BalanceFragment(),"Balance");
viewPager.setAdapter(adapter);
}



}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store