Code cleanup

This commit is contained in:
khanhduytran0 2020-10-08 09:47:54 +07:00
parent ca3da54da0
commit 01d514d9fe
11 changed files with 165 additions and 496 deletions

View File

@ -1,5 +1,3 @@
package com.kdt.filerapi;
/*
* Copyright (C) 2012 Paul Burke
*
@ -16,6 +14,8 @@ package com.kdt.filerapi;
* limitations under the License.
*/
package com.ipaulpro.afilechooser;
import android.content.*;
import android.view.*;
import android.widget.*;
@ -30,7 +30,7 @@ import net.kdt.pojavlaunch.*;
* @author paulburke (ipaulpro)
*
* @addDate 2018-08-08
* @addToMyProject khanhduy032 (kdt032)
* @addToMyProject khanhduy032
*/
public class FileListAdapter extends BaseAdapter {

View File

@ -1,170 +1,141 @@
package com.kdt.filerapi;
import android.support.v7.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.support.annotation.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
import com.ipaulpro.afilechooser.*;
import java.io.*;
import java.util.*;
import net.kdt.pojavlaunch.*;
public class FileListView extends LinearLayout
{
//For list view:
private String fullPath;
private TextView mainTv;
private ListView mainLv;
private Context context;
//For list view:
private String fullPath;
private ListView mainLv;
private Context context;
private boolean lockedHome = false;
//For message empty:
private WindowManager emptyWm;
private TextView emptyTv;
//For File selected listener:
private FileSelectedListener listener;
private AlertDialog build;
private String homePath;
//For File selected listener:
private FileSelectedListener listener;
public FileListView(Context context, AlertDialog build) {
super(context);
init(context);
this.build = build;
}
public FileListView(Context context)
{
super(context);
init(context);
}
public FileListView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public FileListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
public void init(final Context context)
{
//Empty message setup:
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.gravity = Gravity.CENTER;
mParams.format = PixelFormat.TRANSLUCENT;
mParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
public FileListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
emptyWm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
public FileListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
emptyTv = new TextView(context);
emptyTv.setText("This folder is empty");
public void init(final Context context) {
//Main setup:
this.context = context;
emptyWm.addView(emptyTv, mParams);
LayoutParams layParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//Main setup:
this.context = context;
setOrientation(VERTICAL);
LayoutParams layParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
mainLv = new ListView(context);
setOrientation(VERTICAL);
//listFileAt(Environment.getExternalStorageDirectory().getPath());
mainTv = new TextView(context);
mainTv.setText("Path: null");
mainLv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
{
// TODO: Implement this method
File mainFile = new File(p1.getItemAtPosition(p3).toString());
if (p3 == 0) {
parentDir();
} else {
listFileAt(mainFile.getAbsolutePath());
}
}
});
mainLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> p1, View p2, int p3, long p4)
{
// TODO: Implement this method
File mainFile = new File(p1.getItemAtPosition(p3).toString());
if (mainFile.isFile()) {
listener.onFileLongClick(mainFile, mainFile.getAbsolutePath());
return true;
}
return false;
}
});
addView(mainLv, layParam);
}
public void setFileSelectedListener(FileSelectedListener listener)
{
this.listener = listener;
}
public void listFileAt(final String path)
{
try{
final File mainPath = new File(path);
if(mainPath.exists()){
if(mainPath.isDirectory()){
if(!lockedHome){
homePath = path;
lockedHome = true;
}
fullPath = path;
mainLv = new ListView(context);
File[] listFile = mainPath.listFiles();
FileListAdapter fileAdapter = new FileListAdapter(context);
if(!path.equals(homePath)){
//fileAdapter.add(new File(path, "Path=\""+path+"\".noEquals(homePath=\""+homePath+"\")"));
fileAdapter.add(new File(path, ".."));
}
listFileAt(Environment.getExternalStorageDirectory().getPath());
if(listFile.length != 0){
Arrays.sort(listFile, new SortFileName());
for(File file : listFile){
fileAdapter.add(file);
}
}
mainLv.setAdapter(fileAdapter);
build.setTitle(new File(path).getName());
} else {
listener.onFileSelected(mainPath, path);
}
} else {
Toast.makeText(context, "This folder (or file) doesn't exist", Toast.LENGTH_SHORT).show();
refreshPath();
}
} catch (Exception e){
Tools.showError(context, e);
}
}
mainLv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
{
// TODO: Implement this method
File mainFile = new File(p1.getItemAtPosition(p3).toString());
String name = mainFile.getName();
//Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
if(name.equals(".. ")){
parentDir();
}
else{
listFileAt(mainFile.getAbsolutePath());
}
}
});
public String getFullPath(){
return fullPath;
}
addView(mainTv, layParam);
addView(mainLv, layParam);
}
public void setFileSelectedListener(FileSelectedListener listener)
{
this.listener = listener;
}
public void listFileAt(final String path)
{
try{
final File mainPath = new File(path);
if(mainPath.exists()){
if(mainPath.isDirectory()){
fullPath = path;
File[] listFile = mainPath.listFiles();
FileListAdapter fileAdapter = new FileListAdapter(context);
fileAdapter.add(new File(path, ".. "));
if(listFile.length != 0){
Arrays.sort(listFile, new SortFileName());
for(File file : listFile){
fileAdapter.add(file);
}
emptyTv.setVisibility(View.GONE);
}
else{
emptyTv.setVisibility(View.VISIBLE);
}
mainLv.setAdapter(fileAdapter);
mainTv.setText("Path: " + path);
}
else{
String name = mainPath.getName();
// String extension = getExtension(path);
listener.onFileSelected(mainPath, path, name);
}
}
else{
Toast.makeText(context, "This folder (or file) doesn't exist", Toast.LENGTH_SHORT).show();
refreshPath();
}
}
catch(Exception e){
/*
new AlertDialog.Builder(context)
.setTitle("Error")
.setMessage(Log.getStackTraceString(e))
.setPositiveButton("OK", null)
.show();
*/
}
}
public static String getExtension(String path)
{
return getExtension(new File(path));
}
public static String getExtension(File file)
{
return file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("."));
}
public String getFullPath()
{
return fullPath;
}
public void refreshPath()
{
listFileAt(getFullPath());
}
public void parentDir()
{
File pathFile = new File(fullPath);
if(!pathFile.getAbsolutePath().equals(Environment.getExternalStorageDirectory().getAbsolutePath())){
listFileAt(pathFile.getParent());
}
}
public void refreshPath()
{
listFileAt(getFullPath());
}
public void parentDir()
{
File pathFile = new File(fullPath);
if(!pathFile.getAbsolutePath().equals("/")){
listFileAt(pathFile.getParent());
}
}
}

View File

@ -2,7 +2,8 @@ package com.kdt.filerapi;
import java.io.File;
public interface FileSelectedListener
public abstract class FileSelectedListener
{
public void onFileSelected(File file, String path, String name);
public abstract void onFileSelected(File file, String path);
public void onFileLongClick(File file, String path) {}
}

View File

@ -1,112 +0,0 @@
package com.kdt.filermod;
/*
* Copyright (C) 2012 Paul Burke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.util.*;
/**
* List adapter for Files.
*
* @version 2013-12-11
* @author paulburke (ipaulpro)
*
* @addDate 2018-08-08
* @addToMyProject khanhduy032 (kdt032)
*/
public class MFileListAdapter extends BaseAdapter {
private final LayoutInflater mInflater;
private List<File> mData = new ArrayList<File>();
public MFileListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public void add(File file) {
mData.add(file);
notifyDataSetChanged();
}
public void remove(File file) {
mData.remove(file);
notifyDataSetChanged();
}
public void insert(File file, int index) {
mData.add(index, file);
notifyDataSetChanged();
}
public void clear() {
mData.clear();
notifyDataSetChanged();
}
@Override
public File getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return mData.size();
}
public List<File> getListItems() {
return mData;
}
/**
* Set the list items without notifying on the clear. This prevents loss of
* scroll position.
*
* @param data
*/
public void setListItems(List<File> data) {
mData = data;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null)
row = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView view = (TextView) row;
// Get the file at the current position
final File file = getItem(position);
// Set the TextView as the file name
view.setText(file.getName());
return row;
}
}

View File

@ -1,167 +0,0 @@
package com.kdt.filermod;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
import java.io.*;
import java.util.*;
import net.kdt.pojavlaunch.*;
public class MFileListView extends LinearLayout
{
//For list view:
private String fullPath;
private ListView mainLv;
private Context context;
private boolean lockedHome = false;
//For File selected listener:
private MFileSelectedListener listener;
private AlertDialog build;
private String homePath;
public MFileListView(Context context, AlertDialog build)
{
super(context);
init(context);
this.build = build;
}
public MFileListView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public MFileListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
public void init(final Context context)
{
//Main setup:
this.context = context;
LayoutParams layParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
setOrientation(VERTICAL);
mainLv = new ListView(context);
//listFileAt(Environment.getExternalStorageDirectory().getPath());
mainLv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
{
// TODO: Implement this method
File mainFile = new File(p1.getItemAtPosition(p3).toString());
String name = mainFile.getName();
//Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
if(name.equals(".. ")){
parentDir();
}
else{
listFileAt(mainFile.getAbsolutePath());
}
}
});
mainLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> p1, View p2, int p3, long p4)
{
// TODO: Implement this method
File mainFile = new File(p1.getItemAtPosition(p3).toString());
String name = mainFile.getName();
//Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
if(mainFile.isFile()){
String extension = getExtension(mainFile.getAbsolutePath());
listener.onFileLongClick(mainFile, mainFile.getAbsolutePath(), name, extension);
return true;
}
return false;
}
});
addView(mainLv, layParam);
}
public void setFileSelectedListener(MFileSelectedListener listener)
{
this.listener = listener;
}
public void listFileAt(final String path)
{
try{
final File mainPath = new File(path);
if(mainPath.exists()){
if(mainPath.isDirectory()){
if(!lockedHome){
homePath = path;
lockedHome = true;
}
fullPath = path;
File[] listFile = mainPath.listFiles();
MFileListAdapter fileAdapter = new MFileListAdapter(context);
if(!path.equals(homePath)){
//fileAdapter.add(new File(path, "Path=\""+path+"\".noEquals(homePath=\""+homePath+"\")"));
fileAdapter.add(new File(path, ".. "));
}
if(listFile.length != 0){
Arrays.sort(listFile, new MSortFileName());
for(File file : listFile){
fileAdapter.add(file);
}
}
mainLv.setAdapter(fileAdapter);
build.setTitle(new File(path).getName());
}
else{
String name = mainPath.getName();
String extension = getExtension(path);
listener.onFileSelected(mainPath, path, name, extension);
}
}
else{
Toast.makeText(context, "This folder (or file) doesn't exist", Toast.LENGTH_SHORT).show();
refreshPath();
}
}
catch(Exception e){
/*
new AlertDialog.Builder(context)
.setTitle("Error")
.setMessage(Log.getStackTraceString(e))
.setPositiveButton("OK", null)
.show();
*/
}
}
public static String getExtension(String path)
{
return getExtension(new File(path));
}
public static String getExtension(File file)
{
return file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("."));
}
public String getFullPath()
{
return fullPath;
}
public void refreshPath()
{
listFileAt(getFullPath());
}
public void parentDir()
{
File pathFile = new File(fullPath);
if(!pathFile.getAbsolutePath().equals(Environment.getExternalStorageDirectory().getAbsolutePath())){
listFileAt(pathFile.getParent());
}
}
}

View File

@ -1,9 +0,0 @@
package com.kdt.filermod;
import java.io.File;
public interface MFileSelectedListener
{
public void onFileSelected(File file, String path, String nane, String extension);
public void onFileLongClick(File file, String path, String nane, String extension);
}

View File

@ -1,13 +0,0 @@
package com.kdt.filermod;
import java.io.*;
import java.util.*;
public class MSortFileName implements Comparator<File>
{
@Override
public int compare(File f1, File f2) {
return f1.getName().compareToIgnoreCase(f2.getName());
}
}

View File

@ -5,15 +5,15 @@ import android.os.*;
import android.support.design.widget.*;
import android.support.v4.widget.*;
import android.support.v7.app.*;
import android.support.v7.preference.*;
import android.view.*;
import android.widget.*;
import com.google.gson.*;
import com.kdt.filerapi.*;
import java.io.*;
import java.util.*;
import net.kdt.pojavlaunch.value.customcontrols.*;
import android.support.v7.preference.*;
import net.kdt.pojavlaunch.*;
import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.value.customcontrols.*;
public class CustomControlsActivity extends AppCompatActivity
{
@ -106,13 +106,13 @@ public class CustomControlsActivity extends AppCompatActivity
builder.setPositiveButton(android.R.string.cancel, null);
final AlertDialog dialog = builder.create();
FileListView flv = new FileListView(this);
FileListView flv = new FileListView(this, dialog);
flv.listFileAt(Tools.CTRLMAP_PATH);
flv.setFileSelectedListener(new FileSelectedListener(){
@Override
public void onFileSelected(File file, String path, String name) {
if (name.endsWith(".json")) {
public void onFileSelected(File file, String path) {
if (file.getName().endsWith(".json")) {
setDefaultControlJson(path);
dialog.dismiss();
}
@ -188,13 +188,13 @@ public class CustomControlsActivity extends AppCompatActivity
builder.setPositiveButton(android.R.string.cancel, null);
final AlertDialog dialog = builder.create();
FileListView flv = new FileListView(this);
FileListView flv = new FileListView(this, dialog);
flv.listFileAt(Tools.CTRLMAP_PATH);
flv.setFileSelectedListener(new FileSelectedListener(){
@Override
public void onFileSelected(File file, String path, String name) {
if (name.endsWith(".json")) {
public void onFileSelected(File file, String path) {
if (file.getName().endsWith(".json")) {
loadControl(path);
dialog.dismiss();
}

View File

@ -1,11 +1,11 @@
package net.kdt.pojavlaunch;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.support.design.widget.*;
import android.support.v4.app.*;
import android.support.v4.view.*;
import android.support.v7.app.*;
import android.text.*;
import android.util.*;
@ -17,16 +17,17 @@ import com.kdt.filerapi.*;
import java.io.*;
import java.nio.charset.*;
import java.util.*;
import net.kdt.pojavlaunch.*;
import net.kdt.pojavlaunch.mcfragments.*;
import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.util.*;
import net.kdt.pojavlaunch.value.*;
import org.lwjgl.glfw.*;
import android.app.AlertDialog;
import android.support.v7.app.AlertDialog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.*;
import android.app.*;
//import android.support.v7.view.menu.*;
//import net.zhuoweizhang.boardwalk.downloader.*;
@ -829,12 +830,11 @@ public class MCLauncherActivity extends AppCompatActivity
dialog.setView(edit);
} else {
dialog = builder.create();
FileListView flv = new FileListView(this);
FileListView flv = new FileListView(this, dialog);
flv.setFileSelectedListener(new FileSelectedListener(){
@Override
public void onFileSelected(File file, String path, String name) {
if (name.endsWith(".jar")) {
public void onFileSelected(File file, String path) {
if (file.getName().endsWith(".jar")) {
Intent intent = new Intent(MCLauncherActivity.this, InstallModActivity.class);
intent.putExtra("modFile", file);
startActivity(intent);

View File

@ -6,7 +6,9 @@ import android.graphics.*;
import android.os.*;
import android.support.design.widget.*;
import android.support.v4.app.*;
import android.support.v4.view.*;
import android.support.v7.app.*;
import android.support.v7.app.AlertDialog;
import android.text.*;
import android.util.*;
import android.view.*;
@ -17,16 +19,15 @@ import com.kdt.filerapi.*;
import java.io.*;
import java.nio.charset.*;
import java.util.*;
import net.kdt.pojavlaunch.*;
import net.kdt.pojavlaunch.mcfragments.*;
import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.util.*;
import net.kdt.pojavlaunch.value.*;
import org.lwjgl.glfw.*;
import android.app.AlertDialog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.*;
//import android.support.v7.view.menu.*;
//import net.zhuoweizhang.boardwalk.downloader.*;
@ -836,12 +837,12 @@ public class PojavLauncherActivity extends AppCompatActivity
dialog.setView(edit);
} else {
dialog = builder.create();
FileListView flv = new FileListView(this);
FileListView flv = new FileListView(this, dialog);
flv.setFileSelectedListener(new FileSelectedListener(){
@Override
public void onFileSelected(File file, String path, String name) {
if (name.endsWith(".jar")) {
public void onFileSelected(File file, String path) {
if (file.getName().endsWith(".jar")) {
Intent intent = new Intent(PojavLauncherActivity.this, InstallModActivity.class);
intent.putExtra("modFile", file);
startActivity(intent);

View File

@ -1,7 +1,7 @@
package net.kdt.pojavlaunch;
import android.*;
import android.app.*;
import android.support.v7.app.*;
import android.content.*;
import android.content.pm.*;
import android.os.*;
@ -13,7 +13,6 @@ import android.view.*;
import android.widget.*;
import android.widget.CompoundButton.*;
import com.kdt.filerapi.*;
import com.kdt.filermod.*;
import com.kdt.mojangauth.*;
import java.io.*;
import java.util.*;
@ -21,10 +20,10 @@ import net.kdt.pojavlaunch.update.*;
import net.kdt.pojavlaunch.value.customcontrols.*;
import org.apache.commons.compress.archivers.tar.*;
import org.apache.commons.compress.compressors.xz.*;
import android.app.AlertDialog;
import org.apache.commons.io.*;
import android.support.v7.app.AlertDialog;
public class PojavLoginActivity extends AppCompatActivity
// MineActivity
{
@ -378,12 +377,12 @@ public class PojavLoginActivity extends AppCompatActivity
builder.setCancelable(false);
final AlertDialog dialog = builder.create();
FileListView flv = new FileListView(PojavLoginActivity.this);
FileListView flv = new FileListView(PojavLoginActivity.this, dialog);
flv.setFileSelectedListener(new FileSelectedListener(){
@Override
public void onFileSelected(File file, String path, String name) {
if (name.endsWith(".tar.xz")) {
public void onFileSelected(File file, String path) {
if (file.getName().endsWith(".tar.xz")) {
selectedFile.append(path);
dialog.dismiss();
}
@ -537,24 +536,22 @@ public class PojavLoginActivity extends AppCompatActivity
fhint.setText(R.string.hint_select_account);
// fhint.setLayoutParams(lpHint);
final MFileListView flv = new MFileListView(this, dialog);
final FileListView flv = new FileListView(this, dialog);
// flv.setLayoutParams(lpFlv);
flv.listFileAt(Tools.mpProfiles);
flv.setFileSelectedListener(new MFileSelectedListener(){
flv.setFileSelectedListener(new FileSelectedListener(){
@Override
public void onFileLongClick(final File file, String path, String name, String extension)
public void onFileLongClick(final File file, String path)
{
AlertDialog.Builder builder2 = new AlertDialog.Builder(PojavLoginActivity.this);
builder2.setTitle(name);
builder2.setTitle(file.getName());
builder2.setMessage(R.string.warning_remove_account);
builder2.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface p1, int p2)
{
// TODO: Implement this method
public void onClick(DialogInterface p1, int p2) {
file.delete();
flv.refreshPath();
}
@ -563,7 +560,7 @@ public class PojavLoginActivity extends AppCompatActivity
builder2.show();
}
@Override
public void onFileSelected(File file, final String path, String nane, String extension) {
public void onFileSelected(File file, final String path) {
try {
if (MCProfile.load(path).isMojangAccount()){
MCProfile.updateTokens(PojavLoginActivity.this, path, new RefreshListener(){