如何以编程方式在android中过滤垃圾邮件

时间:2019-06-18 05:52:13

标签: android sms spam-prevention

我一直在研究呼叫阻止程序,我需要从邮件列表中过滤垃圾邮件

如何根据个人,交易,促销类别组织短信 看到此链接

https://play.google.com/store/apps/details?id=com.microsoft.android.smsorganizer&hl=en_IN

例如:-带有otp的消息,带有促销的消息

我有些垃圾邮件过滤,例如来自未知发件人的阻止消息和黑名单中的阻止消息,以及来自私人号码的阻止消息,但是如何在Android中阻止促销消息和otp

我有获取邮件列表的代码,但我无法识别垃圾邮件,这是我的邮件列表代码

   private InternalEventBroadcast internalEventBroadcast = null;
   private SMSConversationsListCursorAdapter cursorAdapter = null;
   private ListView listView = null;
   private int listPosition = 0;
   private String title = null;

   public SMSConversationsListFragment() {
       // Required empty public constructor
   }

   @Override
   public void onActivityCreated(@Nullable Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
       // set activity title
       searchBox.setVisibility(View.GONE);
       BlockAllCallslyt.setVisibility(View.GONE);
       Bundle arguments = getArguments();
       ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
       if (arguments != null && actionBar != null) {
           actionBar.setTitle(arguments.getString(TITLE));
           title = arguments.getString(TITLE);
       }
   }

   @Override
   public void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setHasOptionsMenu(true);
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       if (savedInstanceState != null) {
           listPosition = savedInstanceState.getInt(LIST_POSITION, 0);
       }

       // Inflate the layout for this fragment
       return inflater.inflate(R.layout.fragment_sms_conversations_list, container, false);
   }

   @Override
   public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
       super.onViewCreated(view, savedInstanceState);

       // notify user if permission isn't granted
       Permissions.notifyIfNotGranted(getContext(), Permissions.READ_SMS);
       Permissions.notifyIfNotGranted(getContext(), Permissions.READ_CONTACTS);
       // cursor adapter
       cursorAdapter = new SMSConversationsListCursorAdapter(getContext());
       // on row click listener (receives clicked row)
       cursorAdapter.setOnClickListener(new OnRowClickListener());
       // on row long click listener (receives clicked row)
       cursorAdapter.setOnLongClickListener(new OnRowLongClickListener());

       // add cursor listener to the list
       listView = (ListView) view.findViewById(R.id.rows_list);
       listView.setAdapter(cursorAdapter);
       LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_from_right), 1.0f); //0.5f == time between appearance of listview items.
       listView.setLayoutAnimation(lac);
       listView.startLayoutAnimation();

       // on list empty comment
       TextView textEmptyView = (TextView) view.findViewById(R.id.text_empty);
       listView.setEmptyView(textEmptyView);
       // get the view from search menu item

       searchBox.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
           @SuppressLint("RestrictedApi")
           @Override
           public boolean onQueryTextSubmit(String query) {
               reloadItems(query);
               bottomview.setVisibility(View.VISIBLE);
               ic_more_options.setVisibility(View.VISIBLE);
               return false;
           }

           @SuppressLint("RestrictedApi")
           @Override
           public boolean onQueryTextChange(String newText) {
               if (newText.length() > 0) {
                   reloadItems(newText);
                   bottomview.setVisibility(View.GONE);
                   ic_more_options.setVisibility(View.GONE);

               } else {
                   reloadItems(null);
                   bottomview.setVisibility(View.VISIBLE);
                   ic_more_options.setVisibility(View.VISIBLE);
               }
               return false;
           }
       });


       // init internal broadcast event receiver
       internalEventBroadcast = new InternalEventBroadcast() {
           // SMS was written
           @Override
           public void onSMSWasWritten(String phoneNumber) {
               ContactsAccessHelper db = ContactsAccessHelper.getInstance(getContext());
               int threadId = db.getSMSThreadIdByNumber(getContext(), phoneNumber);
               if (threadId >= 0 &&
                       // refresh cached list view items
                       cursorAdapter.invalidateCache(threadId)) {
                   cursorAdapter.notifyDataSetChanged();
               } else {
                   // reload all list view items
                   loadListViewItems(false, false);
               }
           }

           // SMS was deleted
           @Override
           public void onSMSWasDeleted(String phoneNumber) {
               // reload all list view items
               loadListViewItems(false, false);
           }

           // SMS thread was read
           @Override
           public void onSMSThreadWasRead(int threadId) {
               // refresh cached list view items
               cursorAdapter.invalidateCache(threadId);
               cursorAdapter.notifyDataSetChanged();
           }
       };
       internalEventBroadcast.register(getContext());


       // load SMS conversations to the list
       loadListViewItems(listPosition, true, true);
   }

   @Override
   public void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);
       outState.putInt(LIST_POSITION, listView.getFirstVisiblePosition());
   }

   private void reloadItems(String itemsFilter) {
       //dismissSnackBar();
       int listPosition = listView.getFirstVisiblePosition();
       loadListViewItems(listPosition, false, true);
   }

   @Override
   public void onDestroyView() {
       getLoaderManager().destroyLoader(0);
       internalEventBroadcast.unregister(getContext());
       super.onDestroyView();
   }

   @Override
   public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.main, menu);

       MenuItem menuItem = menu.findItem(R.id.write_message);
       /*Utils.setMenuIconTint(getContext(), menuItem, R.attr.black);*/
       menuItem.setVisible(true);
       menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
           @Override
           public boolean onMenuItemClick(MenuItem item) {
               // open SMS sending activity
               CustomFragmentActivity.show(getContext(),
                       getString(R.string.New_message),
                       SMSSendFragment.class, null);
               return true;
           }
       });

       super.onCreateOptionsMenu(menu, inflater);
   }

   @Override
   public void onPause() {
       super.onPause();
       listPosition = listView.getFirstVisiblePosition();
   }

//----------------------------------------------------------------------

   // On row click listener
   private class OnRowClickListener implements View.OnClickListener {
       @Override
       public void onClick(final View row) {
           // get the clicked conversation
           final SMSConversation sms = cursorAdapter.getSMSConversation(row);
           if (sms != null) {
               String person = (sms.person != null ? sms.person : sms.number);
               // open activity with all the SMS of the conversation
               Bundle prearguments = getArguments();
               Bundle arguments = new Bundle();
               arguments.putString(CONTACT_NAME, person);
               arguments.putString(CONTACT_NUMBER, sms.number);
               arguments.putInt(THREAD_ID, sms.threadId);
               arguments.putInt(UNREAD_COUNT, sms.unread);
               arguments.putString(TITLE, prearguments.getString(TITLE));
               Log.e("action", prearguments.getString(TITLE));
               CustomFragmentActivity.show(getContext(), person,
                       SMSConversationFragment.class, arguments);
           }
       }
   }

   // On row long click listener
   private class OnRowLongClickListener implements View.OnLongClickListener {
       @Override
       public boolean onLongClick(View row) {
           final SMSConversation sms = cursorAdapter.getSMSConversation(row);
           if (sms == null) {
               return true;
           }

           final String person = (sms.person != null ? sms.person : sms.number);
           // create menu dialog
           final Dialog dialog = new Dialog(getContext());
           dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
           dialog.setCancelable(false);
           dialog.setContentView(R.layout.dialog_options);
           dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
           TextView text = (TextView) dialog.findViewById(R.id.dialog_title);
           text.setText(person);

           FloatingActionButton dialogBtn_cancel = (FloatingActionButton) dialog.findViewById(R.id.close_btn);
           dialogBtn_cancel.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   //Toast.makeText(getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
                   dialog.dismiss();
               }
           });

           RelativeLayout item;
           // add menu item of sms deletion
           setimageandheading(R.id.subheading1, getString(R.string.Delete_thread), R.id.subheadingimage_1, getContext().getResources().getDrawable(R.drawable.ic_delete), dialog);
           item = dialog.findViewById(R.id.item1);
           item.setVisibility(View.VISIBLE);
           item.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   if (DefaultSMSAppHelper.isDefault(getContext())) {
                       // remove SMS thread
                       ContactsAccessHelper db = ContactsAccessHelper.getInstance(getContext());
                       if (db.deleteSMSMessagesByThreadId(getContext(), sms.threadId)) {
                           // reload list
                           loadListViewItems(false, true);
                       }
                   } else {
                       Toast.makeText(getContext(), R.string.Need_default_SMS_app,
                               Toast.LENGTH_SHORT).show();
                   }
                   dialog.dismiss();
               }
           });

           final DatabaseAccessHelper db = DatabaseAccessHelper.getInstance(getContext());
           if (db != null) {
               // 'move contact to black list'
               DatabaseAccessHelper.Contact contact = db.getContact(person, sms.number);
               if (contact == null || contact.type != Contact.TYPE_BLACK_LIST) {
                   setimageandheading(R.id.subheading2, getString(R.string.Move_to_black_list), R.id.subheadingimage_2, getContext().getResources().getDrawable(R.drawable.ic_blacklist_dialog), dialog);
                   item = dialog.findViewById(R.id.item2);
                   item.setVisibility(View.VISIBLE);
                   item.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           db.addContact(Contact.TYPE_BLACK_LIST, person, sms.number);
                           dialog.dismiss();
                       }
                   });
               }

               // 'move contact to white list'
               if (contact == null || contact.type != Contact.TYPE_WHITE_LIST) {
                   setimageandheading(R.id.subheading3, getString(R.string.Move_to_white_list), R.id.subheadingimage_3, getContext().getResources().getDrawable(R.drawable.ic_white_list_dialog), dialog);
                   item = dialog.findViewById(R.id.item3);
                   item.setVisibility(View.VISIBLE);
                   item.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           db.addContact(Contact.TYPE_WHITE_LIST, person, sms.number);
                           dialog.dismiss();
                       }
                   });
               }
           }

           dialog.show();

           return true;
       }
   }

//----------------------------------------------------------------------

   // Loads SMS conversations to the list view
   private void loadListViewItems(boolean markSeen, boolean showProgress) {
       int listPosition = listView.getFirstVisiblePosition();
       loadListViewItems(listPosition, markSeen, showProgress);
   }

   // Loads SMS conversations to the list view
   private void loadListViewItems(int listPosition, boolean markSeen, boolean showProgress) {
       if (!isAdded()) {
           return;
       }
       int loaderId = 0;
       ConversationsLoaderCallbacks callbacks =
               new ConversationsLoaderCallbacks(getContext(), listView,
                       listPosition, cursorAdapter, markSeen, showProgress);

       LoaderManager manager = getLoaderManager();
       Loader<?> loader = manager.getLoader(loaderId);
       if (loader == null) {
           // init and run the items loader
           manager.initLoader(loaderId, null, callbacks);
       } else {
           // restart loader
           manager.restartLoader(loaderId, null, callbacks);
       }
   }

   // SMS conversations loader
   private static class ConversationsLoader extends CursorLoader {
       ConversationsLoader(Context context) {
           super(context);
       }

       @Override
       public Cursor loadInBackground() {
           // get all SMS conversations
           ContactsAccessHelper db = ContactsAccessHelper.getInstance(getContext());
           return db.getSMSConversations(getContext());
       }
   }

   // SMS conversations loader callbacks
   private static class ConversationsLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> {
       private ProgressDialogHolder progress = new ProgressDialogHolder();
       private SMSConversationsListCursorAdapter cursorAdapter;
       private Context context;
       private ListView listView;
       private int listPosition;
       private boolean markSeen;
       private boolean showProgress;

       ConversationsLoaderCallbacks(Context context, ListView listView, int listPosition,
                                    SMSConversationsListCursorAdapter cursorAdapter,
                                    boolean markSeen, boolean showProgress) {
           this.context = context;
           this.listView = listView;
           this.listPosition = listPosition;
           this.cursorAdapter = cursorAdapter;
           this.markSeen = markSeen;
           this.showProgress = showProgress;
       }

       @Override
       public Loader<Cursor> onCreateLoader(int id, Bundle args) {
           if (showProgress) {
               progress.show(context, R.string.Loading_);
           }
           return new ConversationsLoader(context);
       }

       @Override
       public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
           cursorAdapter.changeCursor(cursor);

           // scroll list to the saved position
           listView.post(new Runnable() {
               @Override
               public void run() {
                   Cursor cursor = cursorAdapter.getCursor();
                   if (cursor != null && !cursor.isClosed() && cursor.getCount() > 0) {
                       listView.setSelection(listPosition);
                       listView.setVisibility(View.VISIBLE);
                   }
               }
           });

           if (markSeen) {
               // mark all SMS are seen
               new SMSSeenMarker(context).execute();
           }

           progress.dismiss();
       }

       @Override
       public void onLoaderReset(Loader<Cursor> loader) {
           cursorAdapter.changeCursor(null);
           progress.dismiss();
       }
   }

//----------------------------------------------------------------------

   // Async task - marks all SMS are seen
   private static class SMSSeenMarker extends AsyncTask<Void, Void, Void> {
       private Context context;

       SMSSeenMarker(Context context) {
           this.context = context;
       }

       @Override
       protected Void doInBackground(Void... params) {
           ContactsAccessHelper db = ContactsAccessHelper.getInstance(context);
           db.setSMSMessagesSeen(context);
           return null;
       }
   }

   public void setimageandheading(int headingid, String heading, int imageid, Drawable Image, Dialog dialog) {
       TextView subheading1 = dialog.findViewById(headingid);
       subheading1.setText(heading);
       ImageView icon = dialog.findViewById(imageid);
       if (heading.equals(getString(R.string.Move_to_black_list))) {
           icon.setColorFilter(ContextCompat.getColor(getContext(), R.color.black), android.graphics.PorterDuff.Mode.MULTIPLY);
       }
       icon.setImageDrawable(Image);

   }

}```


 [1]: https://play.google.com/store/apps/details?id=com.microsoft.android.smsorganizer&hl=en_IN

0 个答案:

没有答案