Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Added Scratchcard ScannerActivity.java and some layout manipulations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jetsup committed Feb 10, 2023
1 parent 1c38fd0 commit 76b861f
Show file tree
Hide file tree
Showing 27 changed files with 665 additions and 97 deletions.
3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 26 additions & 11 deletions app/src/main/java/com/ngichtech/kenyaussd/CodeContent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.ngichtech.kenyaussd;

import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_NAME_AIRTEL;
import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_NAME_EXT;
import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_NAME_FAIBA;
import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_NAME_SAFARICOM;
import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_NAME_TELKOM;
import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_SLOGAN_EXT;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -21,13 +28,6 @@ public class CodeContent extends AppCompatActivity {
ISPContentAdapter ispContentAdapter;
Map<String, String> ispDeco = new HashMap<>();

{
ispDeco.put("CodeContent", "The Better Option");
ispDeco.put("Airtel", "The Smartphone Network");
ispDeco.put("Telkom", "Moving Forward");
ispDeco.put("Faiba", "I am Future Proof");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -36,12 +36,27 @@ protected void onCreate(Bundle savedInstanceState) {
ussdItemRecycler = findViewById(R.id.ussd_items_recycler);
ussdItemRecycler.setLayoutManager(new LinearLayoutManager(this));

ispNameReceived = getIntent().getStringExtra("ISP_NAME");
ispSloganReceived = getIntent().getStringExtra("ISP_SLOGAN");
Objects.requireNonNull(getSupportActionBar()).setTitle(ispNameReceived);
ispDeco.put(ISP_NAME_SAFARICOM, "The Better Option");
ispDeco.put(ISP_NAME_AIRTEL, "The Smartphone Network");
ispDeco.put(ISP_NAME_TELKOM, "Moving Forward");
ispDeco.put(ISP_NAME_FAIBA, "I am Future Proof");

ispNameReceived = getIntent().getStringExtra(ISP_NAME_EXT);
ispSloganReceived = getIntent().getStringExtra(ISP_SLOGAN_EXT);

ActionBar actionBar = Objects.requireNonNull(getSupportActionBar());
actionBar.setTitle(ispNameReceived);
actionBar.setDisplayHomeAsUpEnabled(true);

sloganHeaderText.setText(ispDeco.get(ispNameReceived));

ispContentAdapter = new ISPContentAdapter(CodeContent.this, ispNameReceived.toLowerCase());
ussdItemRecycler.setAdapter(ispContentAdapter);
}

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return super.onSupportNavigateUp();
}
}
16 changes: 15 additions & 1 deletion app/src/main/java/com/ngichtech/kenyaussd/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.ngichtech.kenyaussd;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.ExperimentalGetImage;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand All @@ -20,6 +23,7 @@
import java.io.File;
import java.util.Objects;

@ExperimentalGetImage
public class MainActivity extends AppCompatActivity {
public static final String TAG = "My_Tag";
private final int CALL_PERMISSION_REQUEST = 1;
Expand Down Expand Up @@ -70,13 +74,23 @@ protected void onCreate(Bundle savedInstanceState) {
ISPAdapter ispAdapter = new ISPAdapter(MainActivity.this);
mainRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mainRecyclerView.setAdapter(ispAdapter);
Button btnOpenCam = findViewById(R.id.btnOpenCamera);
btnOpenCam.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, ScannerActivity.class)));

// No permission required for this
myDirectory = new File(Objects.requireNonNull(getExternalCacheDir().getParentFile()).getPath() + "/Kenya USSD/");
if (!myDirectory.exists()) {
if (!myDirectory.mkdir()) {
Toast.makeText(this, "The folder was not created", Toast.LENGTH_LONG).show();
}
}
// TelephonyManager telephoneCount = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
// Log.w(TAG, "Operator: " + telephoneCount.getSimOperator());
// Log.w(TAG, "OperatorName: " + telephoneCount.getSimOperatorName()); // Safaricom
// Log.w(TAG, "PhoneCount: " + telephoneCount.getPhoneCount()); // 3: (voice, sms, data)
// Log.w(TAG, "Country: " + telephoneCount.getSimCountryIso());
// Log.w(TAG, "Man Code: " + telephoneCount.getManufacturerCode());
// Log.w(TAG, "Country: " + telephoneCount.getSimCarrierIdName());
// Log.w(TAG, "Country: " + telephoneCount.getSimCarrierId());
// Log.w(TAG, "Country: " + telephoneCount.getClass().getName());
}
}
131 changes: 131 additions & 0 deletions app/src/main/java/com/ngichtech/kenyaussd/ScannerActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.ngichtech.kenyaussd;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.widget.EditText;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.Camera;
import androidx.camera.core.CameraControl;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ExperimentalGetImage;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.Preview;
import androidx.camera.core.UseCaseGroup;
import androidx.camera.core.ViewPort;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.common.util.concurrent.ListenableFuture;
import com.ngichtech.kenyaussd.custom.ScanAnalyzer;

import java.util.Objects;
import java.util.concurrent.ExecutionException;

@ExperimentalGetImage
public class ScannerActivity extends AppCompatActivity {

public static String creditNumber = "";
public static boolean dataChanged;
EditText recognizedCreditText;
FloatingActionButton fabCameraFlash;
PreviewView cameraPreview;
CameraControl cameraControl;
private boolean flashOn;

@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
recognizedCreditText = findViewById(R.id.recognizedCreditText);
fabCameraFlash = findViewById(R.id.fabCameraFlash);
cameraPreview = findViewById(R.id.cameraPreview);

fabCameraFlash.setOnClickListener(v -> {
if (flashOn) {
fabCameraFlash.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.baseline_flash_off_24, null));
cameraControl.enableTorch(false);
flashOn = false;
} else {
fabCameraFlash.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.baseline_flash_on_24, null));
cameraControl.enableTorch(true);
flashOn = true;
}
});

bindToCamera();

new Thread(() -> {
while (true) {
if (dataChanged) {
StringBuilder rectifiedString = new StringBuilder();
for (int i = 0; i < creditNumber.length(); i++) {
try {
rectifiedString.append(Integer.parseInt(String.valueOf(creditNumber.charAt(i))));
} catch (NumberFormatException ignore) {
}
}
runOnUiThread(() -> recognizedCreditText.setText(rectifiedString));
dataChanged = false;
}
}
}).start();
}

@RequiresApi(api = Build.VERSION_CODES.P)
private void bindToCamera() {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}, ContextCompat.getMainExecutor(this));
}

@RequiresApi(api = Build.VERSION_CODES.P)
private void bindPreview(ProcessCameraProvider cameraProvider) {
ScanAnalyzer imageAnalyzer = new ScanAnalyzer(this);

ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
.setTargetResolution(new Size(1280, 720))
// .setTargetResolution(new Size(176, 144))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();
imageAnalysis.setAnalyzer(getMainExecutor(), imageAnalyzer);
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
Preview preview = new Preview.Builder().build();
preview.setSurfaceProvider(cameraPreview.getSurfaceProvider());
ViewPort viewPort = cameraPreview.getViewPort();
/* ViewPort, viewPort = new ViewPort.Builder(new Rational( 1, 4), Surface.ROTATION_90).build(); */

Log.w(MainActivity.TAG, "ViewPort: " + cameraPreview.getScaleType() + " <> " + Objects.requireNonNull(viewPort).getAspectRatio());

if (viewPort != null) {
UseCaseGroup useCaseGroup = new UseCaseGroup.Builder()
.addUseCase(preview)
.addUseCase(imageAnalysis)
.setViewPort(viewPort)
.build();
cameraProvider.unbindAll();
Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, useCaseGroup);//, imageAnalysis,preview);
cameraControl = camera.getCameraControl();
cameraControl.setLinearZoom(0f);
}
}

@Override
protected void onResume() {
super.onResume();
flashOn = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.ngichtech.kenyaussd.adapters;

import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_NAME_EXT;
import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_SLOGAN_EXT;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -46,8 +49,8 @@ public void onBindViewHolder(@NonNull ISPAdapter.MyISPViewHolder holder, int pos
holder.ispMainLayout.setOnClickListener(v -> {
Intent intent = new Intent();
intent.setClass(context, CodeContent.class);
intent.putExtra("ISP_NAME", ispNames.get(position));
intent.putExtra("ISP_SLOGAN", ispSlogans.get(position));
intent.putExtra(ISP_NAME_EXT, ispNames.get(position));
intent.putExtra(ISP_SLOGAN_EXT, ispSlogans.get(position));
context.startActivity(intent);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.ngichtech.kenyaussd.adapters;


import static com.ngichtech.kenyaussd.custom.ISPConstants.ARRAY_TYPE;
import static com.ngichtech.kenyaussd.custom.ISPConstants.DRAWABLE_TYPE;
import static com.ngichtech.kenyaussd.custom.ISPConstants.ISP_LOGO_EXT;
import static com.ngichtech.kenyaussd.custom.ISPConstants.USSD_CODE_EXT;
import static com.ngichtech.kenyaussd.custom.ISPConstants.USSD_CODE_NAME_EXT;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.ngichtech.kenyaussd.MainActivity;
import com.ngichtech.kenyaussd.R;

import java.util.ArrayList;
Expand All @@ -27,24 +32,22 @@

public class ISPContentAdapter extends RecyclerView.Adapter<ISPContentAdapter.ContentViewHolder> {
List<String> ussdCodeNames;
List<String> ussdCode;
List<String> ussdCodes;
Context context;
Drawable ispLogoIcon;

@SuppressLint("DiscouragedApi")
public ISPContentAdapter(Context context, String isp_name) {
this.context = context;
ussdCodeNames = new ArrayList<>();
ussdCode = new ArrayList<>();
Log.w(MainActivity.TAG, "ISPContentAdapter: "+ isp_name + "_ussd_code_names");
int codeNamesIdentifier = context.getResources().getIdentifier(isp_name + "_ussd_code_names", "array", context.getPackageName());
ussdCodes = new ArrayList<>();
int codeNamesIdentifier = context.getResources().getIdentifier(isp_name + USSD_CODE_NAME_EXT, ARRAY_TYPE, context.getPackageName());
ussdCodeNames = Arrays.asList(context.getResources().getStringArray(codeNamesIdentifier));
Log.w(MainActivity.TAG, "ISPContentAdapter: "+codeNamesIdentifier +" <> "+ Arrays.toString(context.getResources().getStringArray(codeNamesIdentifier)));

int codeIdentifier = context.getResources().getIdentifier(isp_name + "_ussd_codes", "array", context.getPackageName());
ussdCode = Arrays.asList(context.getResources().getStringArray(codeIdentifier));
int codeIdentifier = context.getResources().getIdentifier(isp_name + USSD_CODE_EXT, ARRAY_TYPE, context.getPackageName());
ussdCodes = Arrays.asList(context.getResources().getStringArray(codeIdentifier));

int drawableIdentifier = context.getResources().getIdentifier(isp_name + "_logo", "drawable", context.getPackageName());
int drawableIdentifier = context.getResources().getIdentifier(isp_name + ISP_LOGO_EXT, DRAWABLE_TYPE, context.getPackageName());
ispLogoIcon = ResourcesCompat.getDrawable(context.getResources(), drawableIdentifier, null);
}

Expand All @@ -58,11 +61,12 @@ public ISPContentAdapter.ContentViewHolder onCreateViewHolder(@NonNull ViewGroup
@Override
public void onBindViewHolder(@NonNull ISPContentAdapter.ContentViewHolder holder, int position) {
holder.ussdCodeName.setText(ussdCodeNames.get(position));
holder.ussdCode.setText(ussdCode.get(position));
holder.ussdCode.setText(ussdCodes.get(position));
holder.ispLogoImage.setImageDrawable(ispLogoIcon);
holder.cardViewLayout.setOnClickListener(v -> {
// Toast.makeText(context, "Click", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Clicked index: " + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
// TODO: Extract this and use it to handle data from user input dialog
Uri dialReqUri = Uri.fromParts("tel", ussdCodes.get(position), null);
context.startActivity(new Intent(Intent.ACTION_CALL, dialReqUri));
});
}

Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/ngichtech/kenyaussd/custom/ISPConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ngichtech.kenyaussd.custom;

public class ISPConstants {
public final static String USSD_CODE_NAME_EXT = "_ussd_code_names";
public final static String USSD_CODE_EXT = "_ussd_codes";
public final static String ISP_LOGO_EXT = "_logo";
public final static String ISP_NAME_EXT = "ISP_NAME";
public final static String ISP_SLOGAN_EXT = "ISP_SLOGAN";
public final static String ISP_NAME_SAFARICOM = "Safaricom";
public final static String ISP_NAME_AIRTEL = "Airtel";
public final static String ISP_NAME_TELKOM = "Telkom";
public final static String ISP_NAME_FAIBA = "Faiba";
public final static String ARRAY_TYPE = "array";
public final static String DRAWABLE_TYPE = "drawable";
}
Loading

0 comments on commit 76b861f

Please sign in to comment.