Skip to content

Commit

Permalink
Whiltelist feature
Browse files Browse the repository at this point in the history
  • Loading branch information
moithepro committed Mar 3, 2023
1 parent 1162212 commit f48522a
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

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

12 changes: 8 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar">
<activity
android:name=".WhitelistActivity"
android:exported="false" />
<activity
android:name=".UsersActivity"
android:configChanges="orientation"
Expand All @@ -17,14 +20,15 @@
android:name=".UnfollowersActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".LoginActivity" android:configChanges="orientation"
android:screenOrientation="portrait"
android:exported="true">
<activity
android:name=".LoginActivity"
android:configChanges="orientation"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>
</activity>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.moithepro.instatoolsandroid;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
Expand All @@ -8,6 +9,7 @@
import android.os.Looper;
import android.os.PersistableBundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
Expand Down Expand Up @@ -41,6 +43,7 @@
public class UnfollowersActivity extends AppCompatActivity {
private LinearLayout ll;
private ArrayList<Button> buttonsList = new ArrayList<>();
private ArrayList<JInstaProfile> unfollowers = new ArrayList<>();
private DrawerLayout dl;
private ActionBarDrawerToggle abdt;
private Toolbar tb;
Expand All @@ -53,6 +56,10 @@ public class UnfollowersActivity extends AppCompatActivity {
private AlertDialog AD;
private SharedPreferences sp;

private enum Mode {UNFOLLOWERS, WHITELIST}

private Mode currentMode = Mode.UNFOLLOWERS;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -67,6 +74,21 @@ protected void onCreate(Bundle savedInstanceState) {
dl.addDrawerListener(abdt);
abdt.syncState();
final NavigationView nav_view = findViewById(R.id.nv);
nav_view.setNavigationItemSelectedListener(item -> {
if (item.getItemId() == R.id.whitelist) {
tb.setTitle("Whitelisted of @" + profile.getUsername());
currentMode = Mode.WHITELIST;
setUnfollowers(unfollowers);
return true;
}
if (item.getItemId() == R.id.profile_menu_unfollowers) {
tb.setTitle("Unfollowers of @" + profile.getUsername());
currentMode = Mode.UNFOLLOWERS;
setUnfollowers(unfollowers);
return true;
}
return super.onOptionsItemSelected(item);
});
Bundle b = getIntent().getExtras();

index = b.getInt(getString(R.string.index_key));
Expand All @@ -87,33 +109,115 @@ protected void onCreate(Bundle savedInstanceState) {
refreshUnfollowers();
else {
Gson gson = new Gson();
List<JInstaProfile> unfollowers = gson.fromJson(json, new TypeToken<ArrayList<JInstaProfile>>() {
ArrayList<JInstaProfile> unfollowers = gson.fromJson(json, new TypeToken<ArrayList<JInstaProfile>>() {
}.getType());
setUnfollowers(unfollowers);
}
}

private void setUnfollowers(List<JInstaProfile> unfollowers) {
private void setUnfollowers(ArrayList<JInstaProfile> unfollowers) {
for (Button b :
buttonsList) {
ll.removeView(b);
}
buttonsList.clear();

for (JInstaProfile p :
unfollowers) {
addButtonToLayout((p.isVerified() ? getString(R.string.verified) : "") + "@" + p.getUsername(), p);
this.unfollowers = unfollowers;
if (currentMode == Mode.UNFOLLOWERS) {
reloadUnfollowers();
} else if (currentMode == Mode.WHITELIST) {
reloadWhitelist();
}
for (Button b :
buttonsList) {
b.setOnClickListener(view -> {
if (view.getTag() instanceof JInstaProfile) {
Uri uri = Uri.parse("https://www.instagram.com/" + ((JInstaProfile) view.getTag()).getUsername()); // missing 'http://' will cause crashed
Uri uri = Uri.parse("https://www.instagram.com/" + ((JInstaProfile) view.getTag()).getUsername()); // missing 'http://' will cause crash
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
b.setOnLongClickListener(view -> {
if (currentMode == Mode.UNFOLLOWERS) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
List<JInstaProfile> whitelist = new ArrayList<>();
String json = sp.getString(getString(R.string.whitelist_key) + index + "_" + profile.getUsername(), "");
if (json != "") {
Gson gson = new Gson();
whitelist = gson.fromJson(json, new TypeToken<ArrayList<JInstaProfile>>() {
}.getType());
}
whitelist.add((JInstaProfile) view.getTag());
Gson gson = new Gson();
String json2 = gson.toJson(whitelist);
sp.edit().putString(getString(R.string.whitelist_key) + index + "_" + profile.getUsername(), json2).apply();
setUnfollowers(unfollowers);
break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("WhiteList?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
} else if (currentMode == Mode.WHITELIST) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
List<JInstaProfile> whitelist = new ArrayList<>();
String json = sp.getString(getString(R.string.whitelist_key) + index + "_" + profile.getUsername(), "");
if (json != "") {
Gson gson = new Gson();
whitelist = gson.fromJson(json, new TypeToken<ArrayList<JInstaProfile>>() {
}.getType());
}
whitelist.remove((JInstaProfile) view.getTag());
Gson gson = new Gson();
String json2 = gson.toJson(whitelist);
sp.edit().putString(getString(R.string.whitelist_key) + index + "_" + profile.getUsername(), json2).apply();
setUnfollowers(unfollowers);
break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Remove from whitelist?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
return false;
});
}
}

private boolean isWhitelisted(JInstaProfile p) {
String json = sp.getString(getString(R.string.whitelist_key) + index + "_" + profile.getUsername(), "");

if (json == "")
return false;
Gson gson = new Gson();
List<JInstaProfile> whitelist = gson.fromJson(json, new TypeToken<ArrayList<JInstaProfile>>() {
}.getType());
for (JInstaProfile profile :
whitelist) {
if (profile.getUsername().equals(p.getUsername()))
return true;
}
return false;
}

private void refreshUnfollowers() {
Expand All @@ -133,7 +237,7 @@ private void refreshUnfollowers() {
mainHandler.post(() -> {
statusText.setText(R.string.refreshing);
});
List<JInstaProfile> unfollowers = new ArrayList<>();
ArrayList<JInstaProfile> unfollowers = new ArrayList<>();
unfollowers.addAll(following);
unfollowers.removeAll(followers);
mainHandler.post(() -> {
Expand All @@ -150,12 +254,7 @@ private void refreshUnfollowers() {
errorUsernameNotExists();
statusText.setText("");
});
} catch (JLoginRequiredException e) {
mainHandler.post(() -> {
errorUserNotAccessible();
statusText.setText("");
});
} catch (JPrivateProfileNotFollowedException e) {
} catch (JLoginRequiredException | JPrivateProfileNotFollowedException e) {
mainHandler.post(() -> {
errorUserNotAccessible();
statusText.setText("");
Expand Down Expand Up @@ -220,6 +319,64 @@ private void errorUserNotAccessible() {
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
// when navigation button is pressed


private void reloadUnfollowers() {
// get whitelist from shared preferences
String json = sp.getString(getString(R.string.whitelist_key) + index + "_" + profile.getUsername(), "");
List<JInstaProfile> whitelist = new ArrayList<>();
if (json.isEmpty()) {

} else {
Gson gson = new Gson();
whitelist = gson.fromJson(json, new TypeToken<List<JInstaProfile>>() {
}.getType());
}

List<JInstaProfile> unfollowers = new ArrayList<>(this.unfollowers);

for (JInstaProfile p :
whitelist) {
unfollowers.remove(p);
}
for (Button b :
buttonsList) {
ll.removeView(b);
}
buttonsList.clear();
for (JInstaProfile p :
unfollowers) {
addButtonToLayout((p.isVerified() ? getString(R.string.verified) : "") + "@" + p.getUsername(), p);
}
currentMode = Mode.UNFOLLOWERS;

}

private void reloadWhitelist() {
// get whitelist from shared preferences
String json = sp.getString(getString(R.string.whitelist_key) + index + "_" + profile.getUsername(), "");
List<JInstaProfile> whitelist = new ArrayList<>();
if (json.isEmpty()) {

} else {
Gson gson = new Gson();
whitelist = gson.fromJson(json, new TypeToken<List<JInstaProfile>>() {
}.getType());
}

for (Button b :
buttonsList) {
ll.removeView(b);
}
buttonsList.clear();
for (JInstaProfile p :
whitelist) {
addButtonToLayout("⭐" + (p.isVerified() ? getString(R.string.verified) : "") + "@" + p.getUsername(), p);
}
currentMode = Mode.WHITELIST;

}

public Button addButtonToLayout(String username, Object tag) {
Button button = new Button(this);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
<string name="login_key">login_key</string>
<string name="password_key">password_key</string>
<string name="username_key">username_key</string>
<string name="whitelist_key">whitelist_key</string>
</resources>
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx1024m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
Expand Down

0 comments on commit f48522a

Please sign in to comment.