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

Refactor MainActivity Layout and Functionality #1

Merged
merged 8 commits into from
Dec 10, 2023
Merged
24 changes: 13 additions & 11 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: gradle
- uses: actions/checkout@v3

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
- name: set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build
37 changes: 35 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Gradle files
.gradle/
build/
.gradlew/
gradlew.bat

# Local configuration file (sdk path, etc)
local.properties

# Log/OS Files
*.log
*.zip
.DS_Store
Thumbs.db

# Android Studio generated files and folders
captures/
Expand All @@ -15,12 +20,16 @@ captures/
*.apk
output.json

# IntelliJ
# IntelliJ IDEA
*.iml
.idea/
misc.xml
deploymentTargetDropDown.xml
render.experimental.xml
workspace.xml
tasks.xml
usage.statistics.xml
shelf/

# Keystore files
*.jks
Expand All @@ -30,4 +39,28 @@ render.experimental.xml
google-services.json

# Android Profiling
*.hprof
*.hprof

# Proguard
proguard/
*.pro

# Generated Files
*.user
*.dat
*.bak

# OS-specific files
.DS_Store (macOS)
Thumbs.db (Windows)

# Crashlytics
crashlytics-build.properties

# Ignore Gradle Wrapper jar
!gradle/wrapper/gradle-wrapper.jar
!gradle/wrapper/gradle-wrapper.properties

# Ignore sensitive data and environment files
secrets.xml
.env
16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {

android {
namespace 'com.example.kisscounter'
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "com.example.kisscounter"
minSdk 24
targetSdk 33
targetSdk 34
versionCode 1
versionName "1.0"

Expand All @@ -20,19 +20,19 @@ android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@
</intent-filter>
</activity>
</application>

</manifest>
117 changes: 83 additions & 34 deletions app/src/main/java/com/example/kisscounter/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,115 @@
package com.example.kisscounter;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static final String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private static final int READ_REQUEST_CODE = 1;
private Uri uri;
private ActivityResultLauncher<Intent> openDocumentLauncher;

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

openDocumentLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
if (data != null) {
uri = data.getData();
}
}
}
);
}
public void ReadTextFile(View view) {
try {
String downloadsPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String filePath = downloadsPath + File.separator + "J.txt";

// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}

File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
public void openFilePicker(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
openDocumentLauncher.launch(intent);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (resultData != null) {
uri = resultData.getData();
readTextFromUri(uri);
}
}
}

BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
private void readTextFromUri(Uri uri) {
try (InputStream inputStream = getContentResolver().openInputStream(uri); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder stringBuilder = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
String fileContents = stringBuilder.toString();

fis.close();

TextView textView = findViewById(R.id.textContent);
textView.setText(fileContents);
textView.setText(stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
}

public void onSearchButtonClick(View view) {
EditText editText = findViewById(R.id.searchWord);
String searchWord = editText.getText().toString();

if (uri != null && !searchWord.isEmpty()) {
countOccurrences(uri, searchWord);
} else {
Toast.makeText(this, "Please select a file and enter a search word", Toast.LENGTH_SHORT).show();
}
}

private void countOccurrences(Uri uri, String searchWord) {
try (InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
int count = 0;
String line;
while ((line = reader.readLine()) != null) {
count += countMatches(line, searchWord);
}
TextView resultTextView = findViewById(R.id.resultText);
String result = getString(R.string.occurrences_count, count);
resultTextView.setText(result);
} catch (IOException e) {
e.printStackTrace();
}
}

private int countMatches(String text, String word) {
int count = 0;
int index = 0;

while ((index = text.indexOf(word, index)) != -1) {
count++;
index += word.length();
}

return count;
}
}
Loading